【问题标题】:Create an element of a struct array using struct pointer function使用结构指针函数创建结构数组的元素
【发布时间】:2014-05-17 16:12:14
【问题描述】:

我一直在尝试编写一个带有指向结构数组的指针的函数。调用它后,我的数组应该有一个新的结构元素。

这是你们的建议改变了我的功能

void addPic(pic *picture_Record, int picNumber){


pic tmp_Pic;
char tmp_fileName[100];
char tmp_description[100];
char tmp_location[100];

if (picture_Record[picNumber].description ==NULL || picture_Record[picNumber].fileName ==NULL||picture_Record[picNumber].location==NULL)
    return;

printf("Enter: Picture Name, Picture Description, Picture Location, Picture People(int)\n");
scanf("%s%s%s%d",tmp_fileName, tmp_description, tmp_location, &picture_Record[picNumber].peopleCount);  

tmp_Pic.fileName = (char*)malloc(strlen(tmp_fileName)+1);
tmp_Pic.description=(char*)malloc(strlen(tmp_description)+1);
tmp_Pic.location = (char*)malloc(strlen(tmp_location)+1);
strcpy(tmp_Pic.fileName, tmp_fileName);
strcpy(tmp_Pic.description, tmp_description);
strcpy(tmp_Pic.location, tmp_location);

picture_Record[picNumber] = tmp_Pic;
free(tmp_Pic.fileName);
free(tmp_Pic.description);
free(tmp_Pic.location);
printf("\nInput Done!\n");

我是这样称呼它的。

 int picNumber = 0
 pic pictureRecord[Maximun_Picture +1]= { "" };

 addPic(&pictureRecord[picNumber], picNumber);
 picNumber++;
 //testing
 printf("%s",pictureRecord[0].location)

这是我的结构。

  typedef struct picture_Data
  {
    char* fileName;
    char* description;
    char* location;
    int peopleCount;
  }pic;

它不起作用并打印我 Null 作为第一个元素的位置。为什么?谁能帮帮我。

【问题讨论】:

    标签: c arrays pointers struct


    【解决方案1】:

    问题在于这一行

    scanf("%s%s%s%d",pic_tmp.fileName, pic_tmp.description, pic_tmp.location, &pic_tmp.peopleCount);
    

    假设pic.tmp 有足够的空间分配给fileNamedescriptionlocation。然而,这些都不是真的,因为所有三个字段都未初始化。

    为了解决这个问题,更改代码以将字符串读取到临时缓冲区中,然后将其复制到动态分配的字符串中。

    这是fileName 的操作方法;你需要对这三个都做同样的事情:

    char tmp_fileName[100];
    char tmp_description[100];
    char tmp_location[100];
    scanf("%99s%99s%99s%d",tmp_fileName, tmp_description, tmp_location, &pic_tmp.peopleCount);
    pic_tmp.fileName = malloc(strlen(tmp_fileName)+1);
    strcpy(pic_tmp.fileName, tmp_fileName);
    ...
    

    您也可以通过单个分配复制struct

    picture_Record[picNumber] = pic_tmp;
    

    不要忘记在每个struct 的所有三个成员上调用free 以避免内存泄漏。

    【讨论】:

    • +1 并且当 OP 在它的时候,首先丢失整个传递索引的东西。 调用者 应该将pictureRecord + picNumber 作为唯一参数传递给addPic()。根本不需要温度。
    • 感谢它确实解决了问题!
    • 再次对不起兄弟,但我的代码不起作用。插入整数后,程序只会崩溃和崩溃。你能看看吗?
    • @user3628117 这是因为您过早地释放指针。释放应该最后发生 - 在您使用完记录结构之后。取消引用已释放的指针是未定义的行为,通常会导致崩溃。
    【解决方案2】:

    You should first have to allocate memory

     void addPic(pic *picture_Record, int picNumber){
     pic pic_tmp;  
     printf("Enter: Picture Name, Picture Description, Picture Location, Picture
     People(int)\n");
     pic_tmp.fileName = (char *)malloc(5);    
     picture_Record[picNumber].fileName = (char *)malloc(5);  
     scanf("%s%d",pic_tmp.fileName, &pic_tmp.peopleCount);  
     printf("\nInput Done!\n");  
     strcpy(picture_Record[picNumber].fileName, pic_tmp.fileName);  
     //picture_Record[picNumber].fileName = pic_tmp.fileName;  
     picture_Record[picNumber].description= pic_tmp.description;  
     picture_Record[picNumber].location = pic_tmp.location;  
     picture_Record[picNumber].peopleCount = pic_tmp.peopleCount;  
     printf("%s,%s",pic_tmp.fileName,picture_Record[picNumber].fileName );  
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多