[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
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).
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?
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.
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
Re: Returning and receiving an unsigned char array
It works perfectly now. Thanks.
Re: Returning and receiving an unsigned char array
Quote:
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 :wave: