2

我在做这件事时遇到了一点麻烦。我正在创建一个程序,该程序将使用结构按 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); 

}
4

5 回答 5

2

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

for (int i=0;i<students;++i)
{
  scanf(%d %d"[...],&infoArray[i].studentID, &infoArray[i].year[...]);
}
于 2013-11-08T15:44:47.970 回答
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 中动态更改数组的大小而不显式重新分配它,因此如果您想要动态解决方案,则必须使用链表解决方案。

于 2013-11-08T15:42:03.867 回答
1

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

infoArray [students];withint 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!
}
于 2013-11-08T15:50:28.417 回答
1

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

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++;
  }
于 2013-11-08T16:37:22.680 回答
0

编译器抱怨是因为

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

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

于 2013-11-08T15:46:31.050 回答