in my phase of (re)learning C, I’m often facing problems with arrays and structures (and pointers). This is a little test I wrote.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct {
char name[10];
} man;
int main (int argc, const char * argv[]) {
int i;
man john;
strcpy(john.name, "john");
// array of structures
man men[10];
strcpy(men[3].name, "john");
printf("*** %s ***\n", men[3].name);
// dynamic array of structures
man *men2;
men2 = malloc(10 * sizeof(man));
strcpy(men2[3].name, "john");
printf("*** %s ***\n", men2[3].name);
// multidimensional array of structures
man men3[10][10];
strcpy(men3[3][3].name, "john");
printf("*** %s ***\n", men3[3][3].name);
// dynamic multidimensional array of structures
man **men4;
men4 = malloc(10 * sizeof(man*));
for (i = 0; i < 10; i++)
men4[i] = malloc(10 * sizeof(man*));
strcpy(men4[3][3].name, "john");
printf("*** %s ***\n", men4[3][3].name);
// array of pointer to structure
man *men5[10];
men5[3] = &john;
printf("*** %s ***\n", men5[3]->name);
// dynamic array of pointers to structure
man **men6;
men6 = malloc(10 * sizeof(*men6));
men6[3] = &john;
printf("*** %s ***\n", men6[3]->name);
// dynamic multidimensional array of pointers to structures
/* ? */
return 0;
}
Can you:
- tell me if all is correct
- explain why in men4 i use
sizeof(man*)but in men6 it issizeof(*men6)(asterisk position) - explain how to make a dynamic multidimensional array of pointers to structures (last point)
- explain me men4? it works, but is seems to me more of an array of pointers to structure than a multidimensional array of structures!
Thanks in advance.
First, your these two cases
and
are conceptually the same. In both cases, you have a pointer to a pointer to
man. You are using those pointers differently. Also, the first one has an error. Instead of:you should have written:
because each
men4[i]is of type “pointer toman“. That error is why I usually prefer mymalloc()calls to be of the form:So, your call would have become:
This is less error-prone.
So, the answer to your question (2) is, you should have use
sizeof(man *)only when allocating formen4, but not inside the loop. (Or, you now know a better way to callmalloc(), so the call outsize the loop becomesmen4 = malloc(10 * sizeof *men4);.)malloc()call as mentioned above.[], since it is a pointer to a pointer that you want (multidimensional), you want two**. So you get:man **(men7[10]);, but since[]binds more tightly than*anyway, you can write it asman **men7[10];.men4is not a multidimensional array nor an array of pointers. It is a pointer to a pointer toman. When you allocate memory formen4, you allocate space for 10 pointers toman. Then, each pointer is allocated space for 10man. The fact that you can indexmen4as a multidimensional array is a result of such allocation. In your loop, eachmen4[i]could have been allocated space for different number ofmanvalues if you would have liked, something which is impossible to do with multidimensional arrays.Pointers are not arrays. Arrays are not pointers. Please read all the questions and answers in section 6 of the C FAQ for details.