> And this alloates pointers to array of characters i.e. strings.
Not quite - all those parentheses matter, and translating them into words requires a good deal of precision.
If you have
Code:
char *q = malloc( 10 * sizeof *q );
q is a pointer to an array of 10 characters
It is the dynamically allocated equivalent of char q[10]
If you have
Code:
char (*p)[20] = malloc( 10 * sizeof *p );
p is a pointer to an array of 10 array-of-20-characters.
It is the dynamically allocated equivalent of char p[10][20]
If you have
Code:
char **p = malloc( 10 * sizeof *p );
p is a pointer to an array of 10 pointers to char.
It is the dynamically allocated equivalent of char *p[10]
However, by itself it is not able to store strings, so you need (as per Quzah's post) to allocate some more memory
Code:
for ( i = 0 ; i < 10 ; i++ ) p[i] = malloc( 20 * sizeof *p[i] );
Now you have the dynamically allocated equivalent of char p[10][20]
Now both types of p can be used to store up to 10 strings, each up to 20 characters in length. So for example, this is valid for both of them
Code:
strcpy( p[0], "hello world" );
So why choose char (*p)[20] over char **p ?
Well the pointer to an array is much easier to allocate (just one malloc call), but the downside is that every string will have space for 20 characters whether you want them or not.
The pointer to a pointer on the other hand is more complicated to allocate, but you get the freedom to allocate memory according to the length of each string stored.
> stream->main_data=(unsigned char (*)[MAD_BUFFER_MDLEN])malloc(MAD_BUFFER_MDLEN);
If this is actual C code, then it is broken.
1. You don't cast malloc in C
2. There is no sizeof in the malloc call, so all you've managed to achieve with this call is the equivalent of a pointer to unsigned char array[1][MAD_BUFFER_MDLEN];
Essentially, you've added a dimension for no reason at all.