开发者

C: Structs and array - overview and help needed

in my phase of (re)learnin开发者_StackOverflow社区g 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:

  1. tell me if all is correct
  2. explain why in men4 i use sizeof(man*) but in men6 it is sizeof(*men6) (asterisk position)
  3. explain how to make a dynamic multidimensional array of pointers to structures (last point)
  4. 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

// dynamic multidimensional array of structures
man **men4;
men4 = malloc(10 * sizeof(man*));
for (i = 0; i < 10; i++)
    men4[i] = malloc(10 * sizeof(man*));

and

// dynamic array of pointers to structure
man **men6;
men6 = malloc(10 * sizeof(*men6));
men6[3] = &john;
printf("*** %s ***\n", men6[3]->name);

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:

    men4[i] = malloc(10 * sizeof(man*));

you should have written:

    men4[i] = malloc(10 * sizeof(man));

because each men4[i] is of type "pointer to man". That error is why I usually prefer my malloc() calls to be of the form:

a = malloc(N * sizeof *a);

So, your call would have become:

men4[i] = malloc(10 * sizeof *men4[i]);

This is less error-prone.

So, the answer to your question (2) is, you should have use sizeof(man *) only when allocating for men4, but not inside the loop. (Or, you now know a better way to call malloc(), so the call outsize the loop becomes men4 = malloc(10 * sizeof *men4);.)

  1. It is all correct, except for the malloc() call as mentioned above.
  2. See above.
  3. You want an array of pointers to struct. To get an array, you need [], 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 as man **men7[10];.
  4. men4 is not a multidimensional array nor an array of pointers. It is a pointer to a pointer to man. When you allocate memory for men4, you allocate space for 10 pointers to man. Then, each pointer is allocated space for 10 man. The fact that you can index men4 as a multidimensional array is a result of such allocation. In your loop, each men4[i] could have been allocated space for different number of man values 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.


  • 2: These are two examples that do the same thing: sizeof(*men6) == sizeof(man*)!
  • 3: Same as the men5 example, but multidimensional.
  • 4: The men4 example is not quite correct. As far as I understand what it should mean, that example must read:

man ***men4; // 10x10 multidimensional
men4 = malloc(10 * sizeof(man**));
for (i = 0; i < 10; i++)
{
    men4[i] = malloc(10 * sizeof(man*));
    for(j = 0; j < 10; ++j)
        men4[i][j] = malloc(sizeof(man));
}    
strcpy(men4[3][3]->name, "john");
printf("*** %s ***\n", men4[3][3]->name);


  1. Semantically correct
  2. It IS an array of pointers, so you have to allocate pointer-sized elements, and size of pointer is sizeof(void*) which is equal to sizeof(man*)
  3. man ***men;
  4. Yes, it is. Should be man **men[10];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜