I am writing a function called take_picture()
. The function will make a camera take a picture and store the data in an array.
I want the function to return the address where the array is located and to return the size of the array.
So this is my function declaration:
uint8_t * take_picture(int *piclength)
My question is what would be the correct/logical data types for the function return type and the argument? considering that they store an address and size of an array? Does a uint8_t
and int
make sense?
Thanks
My question is what would be the correct/logical data types for the function return type and the argument? considering that they store an address and size of an array?
The function return type should be a pointer to an array item. If it is an array of uint8_t
, then the return type should be uint8_t*
. This is also the most correct/safe type for an array of bytes ("raw data").
The most correct type used to describe the size of an array is size_t
, an unsigned integer type that exists specifically for this purpose, and is guaranteed portably to be large enough to hold an array size of the given system.
uint8_t* take_picture (size_t* piclength);
or alternatively
void take_picture (uint8_t* picture, size_t* piclength);
unsigned int size; // As the image size is always positive and is large.
uintptr_t address; // if this is available in stdint.h
or
unsigned int address; //it is capable of storing a pointer size.
address = take_picture(&size); //take_picture if returns uintptr_t
If you do not know what take_picture returns, then you cast it like what you have done:
address = (unsigned *int)take_picture(& size);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.