gcc 4.4.3 c89
I am wondering why I can't allocate the size of the array when initializing an array of pointers to char.
I get the following error:
variable-sized object may not be initialized
This works ok. However, the sizeof of will return 4 bytes as a char * is 4 bytes in size. Which is no good, as its not the actual size that I want.
void inc_array(const char * const src, size_t size)
{
/* Array of pointers */
char *dest[sizeof(src)] = {0};
}
However, this is what I want to do is pass the actual size and use that to initialize the length of the array.
void inc_array(const char * const src, size_t size)
{
/* Array of pointers */
char *dest[size] = {0};
}
What is the difference when sizeof of returns a size_t and I am passing a size_t?
Many thanks for any suggestions,
Use malloc to allocate dynamically sized arrays.
You must also ensure that malloced data is eventually freed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With