【问题标题】:How do you assign values entered in a structure to an array?如何将结构中输入的值分配给数组?
【发布时间】:2013-11-20 16:32:01
【问题描述】:

我在执行此操作时遇到了一点麻烦。我正在创建一个程序,该程序将使用结构按 ID 号存储个人信息。然后我需要将其存储在一个数组中,然后使用 for 循环(简单)搜索它。每当我尝试编译时,我都会收到一条错误消息,提示请求成员“blah blah”,而不是结构或联合。

我在最后的 printf 语句中收到此错误。

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

struct infoStruct  
{
  int studentID;
  int year;
  int month;
  int day;
  int phone;
  int end;
};

int main (void)
{
  int students = 0;

struct infoStruct *info = NULL;

  while (info.end != -1) {
    students = students + 1;
    printf("Enter student information (ID, day, month, year, phone)\n");
    printf("Enter -1 following the phone number to end the process to continue enter 0\n");
    info = malloc(sizeof(struct infoStruct) * students);
    scanf("%d %d %d %d %d %d", &info.studentID, &info.day, &info.month, &info.year, &info.phone, &info.end);
  }

  printf("You entered %d student(s)\n", students);

  printf("Enter Student ID\n"); 
  scanf("%d", info.studentID); 

}

【问题讨论】:

标签: c arrays for-loop structure


【解决方案1】:

这个:

int students = 0;
...
int infoArray [students];
...
while()
{
  students = students + 1;
  ...
}

不会工作。您必须先验声明数组的大小,并且如果没有显式重新分配它,它就无法在运行时更改。

你可以做的是:

printf("What's the number of students you want?\n");
scanf("%d", &students);
...
int infoArray [students];
...
while(i++ < students)
{
  // collect the data...
  ...
}

由于您无法在 C 中动态更改数组的大小,而无需显式重新分配它,因此如果您想要动态解决方案,则必须使用 linked list 解决方案。

【讨论】:

  • 是的,但必须实现此功能。简单地调用 infoArray[students++] 是不行的。
  • 不,需要重新分配,初始化然后用++访问它;我只是指出您提供了错误的信息,数组的大小可以通过扩展或缩小的方式进行更改,无需说“数组的大小不能在 c 中动态更改”,因为它是错误的。
【解决方案2】:

首先,您的数组应该是infoStruct 类型。 你应该知道students 的值。然后您可以执行以下操作:

for (int i=0;i<students;++i)
{
  scanf(%d %d"[...],&infoArray[i].studentID, &infoArray[i].year[...]);
}

【讨论】:

    【解决方案3】:

    编译器抱怨是因为

    printf("输入学生证", infoArray.studentID);

    将 infoArray.studentID 视为结构成员,但事实并非如此。

    【讨论】:

      【解决方案4】:

      infoArray 被声明为int。它不提供结构元素,如 infoArray.studentID。这会导致编译器抱怨...不是结构或联合。

      infoArray [students];int students = 0; 也是有问题的。

      你会得到(重写以适应需要。但是,有一些悬而未决的问题):

      #include <stdio.h>
      #include <stdlib.h>
      
      #define MAX_STUDENTS 200
      
      typedef struct {
        int studentID;
        int year;
        int month;
        int day;
        int phone;
        int end;
      } info_TYPE;
      
      int main (void)
      {
        info_TYPE infoArray [MAX_STUDENTS];
        int students = 0;
      
        while (infoArray[students].end != -1) {
          printf("Enter student information (ID, day, month, year, phone)\n");
          printf("Enter -1 following the phone number to end the process to continue enter 0\n");
          scanf("%d %d %d %d %d %d", &infoArray[students].studentID, &infoArray[students].day, &infoArray[students].month, &infoArray[students].year, &infoArray[students].phone, &infoArray[students].end);
          students = students + 1;
          if (students >= MAX_STUDENTS) break;
        }
        if (infoArray[students - 1].end = -1) printf("You entered %d student(s)\n", students);
      
        printf("Enter Student ID", infoArray[students - 1].studentID); 
        // no idea what this line was for, presumably another previous attempt.
        scanf("%d", infoArray[students - 1].studentID); 
        // getting tired to follow the speed at which the question is modified, presumably last edit here!
      }
      

      【讨论】:

      • 这并没有真正帮助我,因为我仍然需要将输入的信息存储到一个数组中。因此,当我尝试使用 malloc 执行此操作时,我收到一条错误消息,告诉我 studentID 不是结构或联合。
      • @user2172993:我已经修改了结果存储在 infoArray 中的代码。
      • Anyway info[students] is out of bounds ,如果你想访问这个数组的最后一个单元格,请使用 info[students-1] 因为 c 中的数组从 0 开始。你要做的是在 for(I=0 ;I
      • 另外我建议使用 tupedef struct 它会更容易:)
      • 你应该从学生=1开始;你的第一个任务也是在分配数组之前会导致崩溃。从学生 =1 开始;分配它,获取输入,然后使用 realoc 函数重新分配它(如果您需要添加更多学生)。就这么简单
      【解决方案5】:

      “信息”是一个指针。所以要访问它的值,你应该取消引用它。当您使用结构数组时,您可以通过以下方式进行操作:

      scanf("%d %d %d %d %d %d", &info[some_counter].studentID, &info[some_counter].day, &info[some_counter].month, &info[some_counter].year, &info[come_counter].phone, &info[come_counter].end);
      

      此外,最好使用 realloc() 调用来更改结构数组的大小并以某种方式修复循环结束条件。现在它会崩溃,因为 info 是 NULL 并且在开始之前你正试图取消引用这个 NULL 指针。
      带有 realloc() 的循环可能类似于以下代码:

        while (true) {
          printf("Enter student information (ID, day, month, year, phone)\n");
          printf("To finish enter word \"end\"\n");
      
          struct infoStruct *new_info = realloc(info, (students+1)*sizeof(struct infoStruct));
          if (new_info == NULL) {
            printf("Out of memory! errno = %s\n", strerror(errno));
            break;
          } else {
            info = new_info;
          }
          result = scanf("%d %d %d %d %d %d", 
              &info[students].studentID,
              &info[students].day, 
              &info[students].month, 
              &info[students].year, 
              &info[students].phone, 
              &info[students].end
                        );
      
          if (result != 6) {
            students--;
            info = realloc(info, sizeof(struct infoStruct) * students);
            break;
          }
      
          students++;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-24
        • 2022-12-05
        • 2017-05-17
        • 1970-01-01
        • 2022-11-27
        • 1970-01-01
        • 2019-08-02
        • 2020-04-02
        相关资源
        最近更新 更多