首页 > 解决方案 > 我在初始化连接到结构数组的变量时遇到问题

问题描述

我有这个项目,我必须用 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;
}

标签: c

解决方案


将无效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);

推荐阅读