gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:44:02 26 4
gpt4 key购买 nike

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com