Defining an array of structures in C?

前端 未结 3 792
温柔的废话
温柔的废话 2021-01-22 17:51

main.h

#define  DATA  struct   data
DATA
{
  int id;
  char data;
}

main.c

DATA *listOfData[100];

So at this

3条回答
  •  清歌不尽
    2021-01-22 18:29

    I'm not sure why printf would just freeze, but there's a couple of things wrong with this. First, all the pointers in your DATA* array are uninitialized. You probably intended to make an array of DATA, instead of an array of DATA pointers:

    DATA listOfData[100];
    

    You also didn't end the struct with a semicolon, so it seems unlikely that this would even compile:

    #define  DATA  struct   data
    DATA
    {
      int id;
      char data;
    };
    

    Finally, you're using printf in a rather unsafe way; the first argument needs to be a format string, or you can get weird behavior if the first argument has a % in it:

    printf("%c\n", listOfData[5].data);
    

提交回复
热议问题