c - 如何将结构中输入的值分配给数组?

标签 c arrays for-loop structure

我在做这件事时遇到了一些麻烦。我正在创建一个程序,它将使用结构按 ID 号存储个人信息。我需要将 then 存储在一个数组中,然后使用 for 循环搜索它(简单)。每当我尝试编译时,都会收到一条错误消息,指出在非结构或 union 中请求成员“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); 

}

最佳答案

首先,您的数组应为 infoStruct 类型。 您应该了解学生的值(value)。然后你可以这样做:

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

关于c - 如何将结构中输入的值分配给数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19862885/

相关文章:

c - 在 C 中存储单词和行列表的最佳数据类型是什么?

php - 需要帮助设置 json 数组

javascript - 用数组动态填充多维数组

PostgreSQL:使用for循环迭代表行,根据当前行检索列值

将数字转换为整数

c - 在 Clion IDE 中将 EOF 发送到标准输入

每个工作表循环的 Excel VBA

c - 使用 'for' 并将指针作为循环变量时的无限循环

c - 在 glib 中打印 utf8

php - 如何从数组中删除空值?