2D arrays:dynamic allocation and freeing
hi all,
The way to dynamically allocate a 2D array is :
int a(*)[5];
a=(int (*)[5])malloc(sizeof(int)*5);
This will create array of pointer to arrays.
We can access elements as a 2D array
a[1][1] with access particular element.
Question:
1.
After allocating space for pointers, is it not the case that i need to allocate space for the array they are pointing to?, as we usually do for a pointer to point to array of elements.
2.
Is my way of freeing ok
for(i=0;i<5;i++)
free(a[i]);
Because it will free the pointers , what about the elements associated with those pointers.
where have i found int (*)[5];
Sorry for that message above:
This is valid syntax.
Actually, i was going through code for mp3, at that time i found this:
[/QUOTE][/PHP][/CODE]
struct mad_stream
{
....
....
unsigned char (*main_data)[MAD_BUFFER_MDLEN];
....
};
struct mad_stream *stream;
stream->main_data=(unsigned char (*)[MAD_BUFFER_MDLEN])malloc(MAD_BUFFER_MDLEN);
[CODE][PHP][QUOTE]
And this alloates pointers to array of characters i.e. strings.
In this array lots of strings can be stored.
Am I wrong with the above statement?
If yes, do correct me.
regards
bravetanveer