C 初始化连接到结构数组的变量时出现问题
我有一个项目,我必须用C语言为学生的数据创建一个结构数组,而我的变量arr_student似乎无法通过这个警告。警告说我没有初始化变量,每当我尝试调试它时,IDE都会说它有一个内存错误,涉及到它的去向。我希望能够声明它,并将其用作获取在结构数组中创建的变量的方法。如果有人知道我可能会错过什么,那将不胜感激C 初始化连接到结构数组的变量时出现问题,c,C,我有一个项目,我必须用C语言为学生的数据创建一个结构数组,而我的变量arr_student似乎无法通过这个警告。警告说我没有初始化变量,每当我尝试调试它时,IDE都会说它有一个内存错误,涉及到它的去向。我希望能够声明它,并将其用作获取在结构数组中创建的变量的方法。如果有人知道我可能会错过什么,那将不胜感激 #include <stdio.h> #include <string.h> #define numero 2 //This
#include <stdio.h>
#include <string.h>
#define numero 2 //This number is the limiter for the number of students
struct Student
{ //Define struct array with student information
char id[50];
char gpa[50];
char address[50];
char phone_number[50];
char first_name[50];
char last_name[50];
};
int main(struct Student arr_student[numero])
{
int i; //Counter
char tempvalue[50]; //Temporary variable to store data in the array
char search[50]; //Search value input by user
int result = 1; //Initialized result to false
for (i = 0; i < numero; i++)
{
//Asks user for input on student information.
printf("\nEnter the information for student %d\n\n", i + 1);
printf("\nEnter first name: ");
scanf("%s", tempvalue);
printf("%s", tempvalue);
//prints value to verify if tempvalue recieved
strcpy(arr_student[i].first_name, tempvalue);
//error begins with the arr_student being underlined
printf("\nEnter last name: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].last_name, tempvalue);
printf("\nEnter student id: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].id, tempvalue);
printf("\nEnter student gpa: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].gpa, tempvalue);
printf("\nEnter student address: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].address, tempvalue);
printf("\nEnter student phone number: ");
scanf("%s", tempvalue);
strcpy(arr_student[i].phone_number, tempvalue);
}
printf("Enter the last name of the student you wish to examine data for: ");
//Asks input from the user for a name to search the data for
scanf("%s", search);
for (i = 0; i < numero; i++)
{
result = strcmp(search, arr_student[i].last_name);
}
if (result == 0) //A match is found in the array
{
printf("Here is the data on the student: %s", search);
printf("First Name\t Last Name\t ID\t GPA\t Address\t Phone Number\n");
//Prints out student information
printf("%s\t%s\t%s\t%s\t%s\t%s\n",
arr_student[i].first_name, arr_student[i].last_name, arr_student[i].id,
arr_student[i].gpa, arr_student[i].address, arr_student[i].phone_number);
}
else
{
printf("The name you have entered is not in our system, please try again");
return;
}
return 0;
}
#包括
#包括
#定义数字2//此数字是学生人数的限制
结构学生
{//使用学生信息定义结构数组
字符id[50];
char-gpa[50];
字符地址[50];
电话号码[50];
char first_name[50];
char last_name[50];
};
int main(结构学生协议学生[numero])
{
int i;//计数器
char tempvalue[50];//在数组中存储数据的临时变量
char search[50];//用户输入的搜索值
int result=1;//将结果初始化为false
对于(i=0;i
将无效的main()
定义从
int main(struct Student arr_student[numero])
{
到
使用宽度限制输入
char tempvalue[50];
// scanf("%s", tempvalue);
scanf("%49s", tempvalue);
将无效的
main()
定义从
int main(struct Student arr_student[numero])
{
到
使用宽度限制输入
char tempvalue[50];
// scanf("%s", tempvalue);
scanf("%49s", tempvalue);
旁白:修复
返回代码>在main()
中。建议返回-1
。旁白:修复返回代码>在main()
中。建议返回-1
。