指针数组结构 [英] array of pointers to structures

查看:133
本文介绍了指针数组结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如果我的code是正确的。我需要声明一个指针数组结构,创建一个新的结构和分配值,并打印出来。在我看来,我不是正确的声明指针数组。我需要知道我在做什么错。谢谢
我得到这个编译错误:错误:'人'未申报(在一次使用此功能)
我也试图插入结构数据*名单;入主但它难道不工作

 的char *书[] = {X,Y,Z,};
     整型数字[] = {1,2,3};     结构数据= {字符* BOOKNAME; INT booknumber;};     功能(字符* x,int y)对
     {
       静态诠释计数;       结构数据*名单[3];       //创建一个新的结构
       列表[统计] =(数据结构*)malloc的(的sizeof(结构数据));       //参数赋值
       列表 - > BOOKNAME = X;
       列表 - > booknumber = Y;       算上++;
     }     诠释的main()
     {
       结构数据*名单[3];       INT I;
       对于(I = 0; I&下; 3;我+ +)
       {
         功能(书[I]中,编号[I]);         的printf(名称:%C数:%d,名单[I] - > BOOKNAME,列表[我] - > booknumber);
       }


解决方案

请更改下面的一段code的

  //声明指向结构的数组//
     结构数据*名单;
    //没有编译
    //结构数据*名单[3]; --->有这种说法没有任何问题。
   //创建一个新的结构
   名单=(数据结构*)malloc的(的sizeof(结构数据)); ---> //这个语句应该编译错误,由于结构数据*名单的申报[3]

 结构数据*名单[100]; //声明一个数组指针,以结构
//对于阵列中的每个元素分配存储器
列表[统计] =(数据结构*)malloc的(的sizeof(结构数据));

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; 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);
       }

解决方案

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) ); 

这篇关于指针数组结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆