CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2017
    Posts
    17

    [RESOLVED] Returning and receiving an unsigned char array

    Why is this giving me a problem? How can I fix it? I'm basically passing a size containing a value of 2 to the makeArray function. Then I make an unsigned char* array Inside the makeArray function, fill its first two subscript, then return that array to main.cc.

    Error:
    main.cc:13:8: error: array type 'unsigned char *[size]' is not assignable

    array = makeArray(size);

    ~~~~~ ^

    1 error generated.

    make: *** [main.o] Error 1


    main.cc
    Code:
    	int size = 2;
    
    	unsigned char* array[size];
    
    	array = makeArray(size);
    
    
    	for (int i = 0; i < size; i++) {
    		printf("%s\n", array[i]);
    
    	} // end of for loop
    functions.cc
    Code:
    unsigned char* makeArray(int size) {
        unsigned char* array = new unsigned char[size];
    
        // fill in the array
        array[0] = '1';
        array[1] = '0';
    
        // return array
        return array;
    
    } // end of makeArray
    Last edited by asilvester635; February 16th, 2017 at 03:44 PM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Returning and receiving an unsigned char array

    is this c code as opposed to c++ code?

    Code:
        unsigned char* array[size];
    array is an array of type unsigned char * (pointer to unsigned char) which has 2 elements.

    Code:
        array = makeArray(size);
    makeArray() returns a type of unsigned char*.

    So either you mean (a)
    Code:
        unsigned char* array[size];
        array[0] = makeArray(size);
    or (b)
    Code:
       unsigned char* array;
       array = makeArray(size);
    I suspect you mean (b).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jan 2017
    Posts
    17

    Re: Returning and receiving an unsigned char array

    Hey, I'm programming in C. Yes I meant (b). Ultimately I want to store the pointer to an unsigned char* array that I made in makeArray() into an unsigned char* array that I created in main.cc. How do I achieve that?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Returning and receiving an unsigned char array

    Do you mean this
    Code:
        unsigned char* array[3];
        array[0] = makeArray(size1);
        array[1] = makeArray(size2);
        array[2] = makeArray(size3);
    array has 3 elements each of which are a pointer to an unsigned char* . Each element of array is then assigned to the return value of makeArray().

    Note that you should release the memory allocated in makeArray() when no longer required.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jan 2017
    Posts
    17

    Re: Returning and receiving an unsigned char array

    I got it. I changed the portion where I fill in the array inside my makeArray function to the code below. I declared that the array is of size 2, but I never really initialized the last storage location array[2]. It works now but why does it let me initialize three slots inside the array when I only made it size 2? Aren't we overriding some other storage location? Possibly erasing a piece of memory (might be important data) and replacing it with another value using this code array[2] = '1';?

    Code:
    unsigned char* makeArray(int size) {
        unsigned char* array = new unsigned char[size];
    
        // fill in the array
        array[0] = '1';
        array[1] = '0';
        array[2] = '1';
    
        // return array
        return array;
    
    } // end of makeArray
    Last edited by asilvester635; February 16th, 2017 at 05:51 PM.

  6. #6
    Join Date
    Jan 2017
    Posts
    17

    Re: Returning and receiving an unsigned char array

    It works perfectly now. Thanks.

  7. #7
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Returning and receiving an unsigned char array

    It works now but why does it let me initialize three slots inside the array when I only made it size 2? Aren't we overriding some other storage location? Possibly erasing a piece of memory (might be important data) and replacing it with another value using this code array[2] = '1';?
    Because this is c! There's no run-time checks for array bounds access etc. If you code it syntactically correct, the computer will try to execute the code. Yes, you are overriding other storage locations. It's called buffer overflow. See https://en.wikipedia.org/wiki/Buffer_overflow Welcome to c
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured