c - 我在初始化连接到结构数组的变量时遇到问题
问题描述
我有这个项目,我必须用 C 语言为学生的数据创建一个结构数组,而我的变量 arr_student 似乎无法通过这个警告。警告说我没有初始化变量,每当我尝试调试它时,IDE 都会说它有一个内存错误,涉及到它的去向。我希望能够声明它并将其用作获取在我的结构数组中创建的变量的一种方式。如果有人知道我可能会错过什么,那将不胜感激!
#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;
}
解决方案
将无效main()
定义从
int main(struct Student arr_student[numero])
{
至
int main(void) {
struct Student arr_student[numero] = {0};
使用宽度受限的输入。
char tempvalue[50];
// scanf("%s", tempvalue);
scanf("%49s", tempvalue);
推荐阅读
- autohotkey - 关闭使用 AutoHotKey 不断更改窗口名称的程序?
- mongodb - 将 mongodb 卷从一个容器复制到另一个容器
- javascript - 删除“轴”触发器上的工具提示悬停效果
- c# - 为什么我在 Visual Studio WPF 设计器中看不到字体?
- reactjs - 如何使用功能组件在浏览器中打印 react-chartjs-2 条形图?
- javascript - 根据过滤器更改按钮值
- google-sheets - 数据透视表的替代方案
- spring - 弹簧靴中具有多个 OR 条件的内连接
- android-intent - 如何使用 Xamarin Android 中的存储访问框架从 URL 下载文件并保存到公共外部目录?
- c - 如果我在代码中将函数命名为与库中已存在的函数相同的名称,会发生什么情况