为什么我的程序不会用printf()显示每个元素;?

时间:2015-01-07 17:27:54

标签: c struct

我做了一个非常小的程序,它实际上允许你用X成员创建N个家庭,当我试图显示来自所有家庭的“所有”成员时我不能,它只会显示最后的家庭成员。为什么以及如何解决它?

............................................... .................................................. .................................................. ..................

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

struct family {
    int size;
    struct person *member;
    char address[256];
};

struct person {
   int age;
   char name[30];
};

int main(){
   struct family *member;
   struct person *ptr;
   int j,k;
   int i,n;
   printf("\nEnter family count: ");
   scanf("%d",&j);

   for(k=0;k<j;k++){
      member=(struct family*)malloc(j*sizeof(struct family));


      printf("Enter family member count: ");
      scanf("%d",&n);


      ptr=(struct person*)malloc(n*sizeof(struct person));
      for(i=0;i<n;++i){
         printf("\nEnter person and the age:\n");
         scanf("%s%d",&(ptr+i)->name, &(ptr+i)->age);
      }
   }
   printf("\nDisplaying Infromation:\n");
   for(i=0;i<n;++i)
      printf("%s\t%d\t\n",(ptr+i)->name,(ptr+i)->age);
   _getch();
   return 0;
}

2 个答案:

答案 0 :(得分:4)

这些行:

for(k=0;k<j;k++){
   member=(struct family*)malloc(j*sizeof(struct family));

确保循环结束时仅保留最后member

这一行

  ptr=(struct person*)malloc(n*sizeof(struct person));

确保循环结束时仅保留最后ptr

您需要的是:

int main(){
   struct family *family_array = NULL;
   int j,k;
   int i,n;

   printf("\nEnter family count: ");
   scanf("%d",&j);

   // Allocate memory for j families.
   family_array = malloc(j*sizeof(struct family));

   // Read the data for each family.
   for(k=0;k<j;k++){

      printf("Enter family member count: ");
      scanf("%d",&n);

      // Make sure to store the size of the family.
      family_array[k].size = n;

      // Allocate memory for the members of the family.
      family_array[k].member = malloc(n*sizeof(struct person));

      // Read the data for each member of the family.
      for(i=0;i<n;++i){
         printf("\nEnter person and the age:\n");
         scanf("%s%d",family_array[k].member[i].name, &(family_array[k].member[i].age));
      }
   }

   printf("\nDisplaying Infromation:\n");
   for(k=0;k<j;k++)
   {
      n = family_array[k].size;
      for(i=0;i<n;++i)
      {
         printf("%s\t%d\t\n", family_array[k].member[i].name, family_array[k].member[i].age);
      }
   }

   // Make sure to deallocate the memory.
   for(k=0;k<j;k++)
   {
      free(family_array[k].member);
   }
   free(family_array);

   _getch();
   return 0;
}

PS 每次使用scanf时都可以添加错误检查代码。假设用户总是提供良好的输入并不是一个好习惯。

答案 1 :(得分:0)

我修复了你的代码,我发现的问题是

  1. struct family *member的第一次分配是错误的,因为它应该在循环之前执行

    member=(struct family*)malloc(j*sizeof(struct family));
    for (k = 0 ; k < j ; k++) ...
    
  2. 您永远不会使用分配的struct family *member;它应该用于存储家庭成员,这就是为什么我将其重命名为family,因此您有一个成员{ {1}}在该结构中,成员计数的size应该存储值

    scanf

    现在该值存储在printf("Enter family member count: "); scanf("%d", &member[k].size); 成员中,因此下一个循环应该像

    size

    for (j = 0 ; j < member[k].size ; j++) ,循环之前应为

    malloc
  3. 您的member[k].member = malloc(member[k].size * sizeof(struct person)); 名称错误,您不应该使用数组的地址

    scanf

    但根据前面提到的问题,你应该这样做

    scanf("%s%d",&(ptr+i)->name, &(ptr+i)->age); -> scanf("%s%d",(ptr+i)->name, &(ptr+i)->age);
    

    修复所有问题后,您可以输出迭代所有scanf("%s%d", member[k].memberj].name, &member[k].member[j].age); 元素和所有member元素的数据。

  4. 这是固定代码

    member->member