You ask 'what goes here' in:
char *b[MAX_SZ] = (WHAT_GOES_HERE?)malloc(10 * sizeof(char[MAX_SZ]));
You want a dynamically allocated pointer to an array of 10 fixed size arrays (of char).
The first problem is "what goes on the LHS of the = sign", because what you've defined is that b is an array of MAX_SZ pointers to char, which is not what you said you wanted.
So, you need:
char (*b)[MAX_SZ] = malloc(10 * sizeof(char[MAX_SZ]));
Now you can refer to b[0] through b[9] as arrays of MAX_SZ characters.
If you want to add a cast (but see the notes and links in my comment), you need to match the type on the left-hand side minus the variable name:
char (*b)[MAX_SZ] = (char (*)[MAX_SZ])malloc(10 * sizeof(char[MAX_SZ]));
I wouldn't post such contorted code without a test, so I created a simple one based on yours and the information above, and ran it under Valgrind and got a clean bill of health.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { MAX_SZ = 256 };
int main(void)
{
/* Pass 1 */
{
char (*b)[MAX_SZ] = malloc(10 * sizeof(char[MAX_SZ]));
strcpy(b[0], "The zeroth");
strcpy(b[9], "The ninth and final element of the array");
printf("From '%s' to '%s', all is well\n", b[0], b[9]);
free(b);
}
/* Pass 2 */
{
char (*b)[MAX_SZ] = (char (*)[MAX_SZ])malloc(10 * sizeof(char[MAX_SZ]));
strcpy(b[0], "The zeroth");
strcpy(b[9], "The ninth and final element of the array");
printf("From '%s' to '%s', all is well\n", b[0], b[9]);
free(b);
}
return 0;
}
The output is boring (sorry):
From 'The zeroth' to 'The ninth and final element of the array', all is well
From 'The zeroth' to 'The ninth and final element of the array', all is well
malloccallocrealloc, 1) always check (!=NULL) the returned value to assure the operation was successful. 2) the returned type, in C, isvoid*which can be assigned to any pointer. Casting just clutters the code, making it more difficult to understand, debug, etc.typedef char Name[MAX_SZ];and related statements: this is giving the keywordchara new meaning ofName[ MAX_SZ ]This 'can' be done' but is a very poor programming practicemalloc(). I side with answers such as this or this, but I learned C beforevoidwas a keyword and on a machine where thechar *to a memory location was a different value from an 'anything else pointer' to the same location so the cast was crucial to getting the code to work at all (on anything other than strings).