简体   繁体   English

指向结构的指针数组

[英]array of pointers to structures

I'm trying to understand if my code is correct. 我想了解我的代码是否正确。 I need to declare an array of pointers to structs, create a new struct and assign the values and print them. 我需要声明一个指向结构的指针的数组,创建一个新的结构并分配值并打印它们。 It seems to me that I'm not declaring array of pointers correctly. 在我看来,我没有正确地声明指针数组。 I need to know what I'm doing wrong. 我需要知道我在做什么错。 Thank you I'm getting this compile error: error: 'people' undeclared (first use in this function) And I've tried to insert struct data *list; 谢谢,我得到了这个编译错误:错误:未声明“ people”(此功能首次使用),并且我尝试插入结构数据* list;。 into main but it wouldnt work 进入主要,但没有用

     char *book[] = { "x", "y", "z",};
     int number[] = { 1, 2, 3};

     struct data = { char *bookname; int booknumber;};

     function(char *x, int y)
     {
       static int count;

       struct data *list[3];

       //creating a new struct 
       list[count] = (struct data*) malloc( sizeof(struct data) );

       //assigning arguments
       list->bookname = x;
       list->booknumber = y;

       count++;
     }

     int main()
     {
       struct data *list[3];

       int i;
       for(i = 0; i < 3; i++)
       {
         function(book[i], number[i]);

         printf("name: %c number: %d", list[i]->bookname, list[i]->booknumber);
       }

Since you want arrays, you need to declare arrays: 由于需要数组,因此需要声明数组:

char *book[] = { "x", "y", "z",};
int number[] = { 1, 2, 3};

Another issue is 另一个问题是

list = (struct data*) malloc( sizeof(struct data) );

//assigning arguments
list[count]->bookname = ...

Here, list is always going to have exactly one element. 在这里, list总是只有一个元素。 So if count is anything other than 0 , you will be accessing an array out of bounds! 因此,如果count0以外的任何0 ,那么您将超出范围访问数组!

Please change the following piece of code 请更改以下代码

    // declaring array of pointers to structs //         
     struct data *list;         
    //not compiling        
    //struct data *list[3]; ---> There is no problem with this statement.        
   //creating a new struct         
   list = (struct data*) malloc( sizeof(struct data) );  ---> //This statement should compilation error due to declaration of struct data *list[3]

to

struct data *list[100]; //Declare a array of pointer to structures  
//allocate memory for each element in the array
list[count] = (struct data*) malloc( sizeof(struct data) ); 

I think you should write: 我想你应该写:

char *book[] = { "x", "y", "z"};

Because in your case you were declaring an array of chars and filling it with pointers, which actually makes no sense. 因为在您的情况下,您声明了一个char数组,并用指针填充了它,这实际上没有任何意义。

In the line of code above, it just means "declare an array of pointers". 在上面的代码行中,它仅表示“声明指针数组”。

Hope it helped... 希望能有所帮助...

These are things are wrong in your program 这些是程序中的错误

struct data = { char *bookname; int booknumber;};

"=" should not be there “ =”不应该在那里

list = (struct data*) malloc( sizeof(struct data) );
list[count]->bookname = x;
list[count]->booknumber = y;

Here you are creating space for single list, so you cant do list[count]-> bookname, it should be list->bookname. 在这里,您正在为单个列表创建空间,因此您不能执行list [count]->书名,它应该是list-> bookname。 Same with booknumber 与书号相同
And list is local to function, you cant access it in main. 并且列表是本地功能,您不能在主目录中访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM