c - 修改学生结构体数组 array c 中的元素

标签 c arrays struct

到目前为止我已经有了这个,我需要帮助来自行修改学生信息。我尝试了几种方法,但似乎无法弄清楚。

#include <stdio.h>

struct student
{
    int id;
    char name[50];
    int age;
    char dept[10];
    float grade;
};

int main()
{
    struct student s[10];

    int i;

    printf("Enter Student Information:\n");

    for (i=0; i<10; i++)
    {
        s[i].id = i+1;
        printf("\nFor ID number %d\n",s[i].id);
        printf("Enter name: "); scanf("%s",s[i].name);
        printf("Enter age: "); scanf("%d", &s[i].age);
        printf("Enter department: "); scanf("%s", s[i].dept);
        printf("Enter grade: "); scanf("%f",&s[i].grade);
        printf("\n");
    }

    return 0;
}

最佳答案

对于你想要的东西来说可能有点过分了,但谁在数呢:

#include <stdio.h>
#include <string.h>

struct student {
    int id;
    char name[50];
    int age;
    char dept[10];
    float grade;
};

void print_student(struct student * students, size_t num_students, int id);
void set_student_by_id(struct student * students, size_t num_students,
                       int id, struct student * new_info);


int main(void) {
    struct student students[5] = {
        {100, "John", 17, "Physics", 3.2},
        {101, "Emily", 16, "Maths", 3.8},
        {102, "Peter", 18, "Drama", 2.1},
        {103, "Doug", 16, "English", 3.3},
        {104, "Sara", 17, "Astronaut", 3.9}
    };

    size_t num_students = sizeof(students) / sizeof(students[0]);

    /*  Print details for student 101 before change  */

    print_student(students, num_students, 101);

    /*  Change details for student 101  */

    struct student new_info = {-1, "Emily", 17, "Farming", 3.9};
    set_student_by_id(students, num_students, 101, &new_info);

    /*  Print details for student 101 after change  */

    print_student(students, num_students, 101);

    /*  Attempt to print details for imaginary student 105  */

    print_student(students, num_students, 105);

    return 0;
}


/*
 *  Prints the details for a selected student.
 *
 *  Arguments:
 *    students - pointer to an array of students
 *    num_students - number of students in the array
 *    id - ID of the student to print
 */

void print_student(struct student * students, size_t num_students, int id) {
    size_t i;
    for ( i = 0; i < num_students; ++i ) {

        /*  Search array for student by ID  */

        if ( students[i].id == id ) {
            printf("Info for student ID %d:\n", id);
            printf("  Name: %s\n", students[i].name);
            printf("  Age: %d\n", students[i].age);
            printf("  Dept: %s\n", students[i].dept);
            printf("  Grade: %.1f\n", students[i].grade);

            break;
        }
    }

    if ( i == num_students ) {
        printf("Student ID %d not found.\n", id);
    }
}


/*
 *  Changes the details for a selected student.
 *
 *  Arguments:
 *    students - pointer to an array of students
 *    num_students - number of students in the array
 *    id - ID of the student to change
 *    new_info - pointer to struct student containing new details
 */

void set_student_by_id(struct student * students, size_t num_students,
                       int id, struct student * new_info) {
    size_t i;
    for ( i = 0; i < num_students; ++i ) {

        /*  Search array for student by ID  */

        if ( students[i].id == id ) {

            /*  Change everything except the ID  */

            strcpy(students[i].name, new_info->name);
            strcpy(students[i].dept, new_info->dept);
            students[i].age = new_info->age;
            students[i].grade = new_info->grade;

            break;
        }
    }

    if ( i == num_students ) {
        printf("Student ID %d not found.\n", id);
    }
}

输出:

paul@local:~/src/c/scratch$ ./student
Info for student ID 101:
  Name: Emily
  Age: 16
  Dept: Maths
  Grade: 3.8
Info for student ID 101:
  Name: Emily
  Age: 17
  Dept: Farming
  Grade: 3.9
Student ID 105 not found.
paul@local:~/src/c/scratch$

关于c - 修改学生结构体数组 array c 中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19579163/

相关文章:

c - 分配给位域时的 GCC 转换警告

c - Arrays如何在 "for"循环中工作(C语言)

PHP多维数组(usort)

php - 使用 PDO、准备好的语句和用 bindParam 替换标记来更新 MySQL;收到 'variables don' t 匹配 token 错误

c - 将结构体数组作为指针传递

C - 从二进制文件读取到任何结构

c - 使用 RDTSC 访问内存中的 ARRAY 时在 C 中出现意外输出

c - 我如何从c中的函数返回多个值

javascript - 如何删除输入数组的最后一个对象?

c - 如何执行结构的指针变量的内存设置,该结构是 C 中另一个结构的指针变量