c - 如何不覆盖字符串并将其放入 typedef 函数中

标签 c

当我进行注册时,我的输出给出了相同的名称和 au-ID。
所以我认为,当用户为第二个学生提供输入时,它会覆盖姓名和 au-ID。

我尝试调用数组 [i] 而不是 [20],并且我尝试使用 malloc 来创建不同的数组并为它们腾出空间。

typedef struct student
{
    char*auID;
    char* name;
    int age;
}student;

int main(void)
{
    //allocate space for student
    int enrolledment = get_int("enrolledment:");
    student students[enrolledment];

    //promt student for name and age and auID
    for(int i=0; i<enrolledment; i++)
    {

         //promt for au-ID
         char getauID[20];
         printf("Enter au-ID: ");
         scanf("%s", getauID);
         students[i].auID=getauID;

        //promt for name
         char getName[20];
         printf("Enter first name: ");
         scanf("%s", getName);
         students[i].name=getName;

        //promt for age
        int getAge;
        printf("Enter age:");
        scanf("%i",&getAge);
        students[i].age=getAge;

    }


    //print students name and age and auID
     for(int i=0; i<enrolledment; i++)
     {
         printf("\n %s is %i and has this au-ID number: %s.\n",students[i].name, students[i].age, students[i].auID);
     }

}

当我将注册设置为 2 时,我期望输出不同的名称和 auID,但实际输出只是相同的名称和 auID,即使 a 输入了不同的名称和 auID

最佳答案

以下建议代码:

  1. 干净地编译
  2. 执行所需的功能
  3. 正确检查 I/O 错误
  4. 正确地将错误消息输出到“stderr”
  5. 为了灵 active 而分开。来自“typedef”的结构定义
  6. 处理任何不可恢复的错误后退出
  7. 由于学生人数不能为 <0,因此使用无符号值作为学生人数
  8. 由于年龄不能为<0,因此使用无符号年龄
  9. 为了便于阅读和理解,采用适当的水平和垂直间距
  10. 使用 C 语言的 VLA(可变长度数组)功能来声明“struct Student”条目数组的长度

现在,建议的代码:

#include <stdio.h>   // printf(), fprintf(), scanf()
#include <stdlib.h>  // exit(), EXIT_FAILURE

#define MAX_FIELD_LEN 20

struct student
{
    char auID[ MAX_FIELD_LEN ];
    char name[ MAX_FIELD_LEN ];
    unsigned age;
};
typedef struct student STUDENT;


int main( void )
{
    //allocate space for students
    unsigned enrolledment;
    if( scanf( "%u", &enrolledment ) != 1 )
    {
        fprintf( stderr, "scanf for number of students failed\n" );
        exit( EXIT_FAILURE );
    }
    // implied else, scanf for number of students successful

    STUDENT students[enrolledment];

    //prompt for  student name and age and auID
    for( unsigned i=0; i<enrolledment; i++ )
    {
        // --> input fields directly into the array entry
        //prompt for au-ID
        printf( "Enter au-ID: " );
        // --> always check for I/O errors and
        // --> limit char array input length
        // --> to one less than the input buffer length
        // --> because '%s' always appends a NUL byte
        if( scanf("%19s", students[i].auID ) != 1 )
        {
            // --> output error messages to 'stderr'
            fprintf( stderr, "scanf for auID failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student ID successful

        //prompt for name
        printf( "Enter first name: " );
        if( scanf("%19s", students[i].name ) != 1 )
        {
            fprintf( stderr,  "scanf for name failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student name successful

        //prompt for age
        printf( "Enter age:" );
        if( scanf( "%u", &students[i].age ) !=1 )
        {
            fprintf( stderr, "scanf for age failed\n" );
            exit( EXIT_FAILURE );
        }
        // implied else, scanf for student age successful
    }


    //print students name and age and auID
    for( unsigned i=0; i<enrolledment; i++ )
    {
        // --> honor the right page margin
        printf( "\n %s is %u and has this au-ID number: %s.\n",
                students[i].name, 
                students[i].age, 
                students[i].auID );
    }
}

关于c - 如何不覆盖字符串并将其放入 typedef 函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57835561/

相关文章:

c - 通过 pipe() 系统调用 : how to imitate pressing enter (during the input) in terminal? 向子进程传输数据

c - 汉诺塔 - 迭代,使用列表

java - 创建自己的POS Tagger

c malloc 分配内存时断言失败

c++ - "inline"关键字与 "inlining"概念

c - 哪个变量存储了 gcc 中 C GENERIC AST 的根节点?

c - 在 C 中,如何以通用方式设置任意大小的 int 的前八位

c - 在不扩展符号链接(symbolic link)的情况下符号链接(symbolic link)相对路径

c - pthread_mutex_lock 为什么不像往常一样阻塞线程

c - Linux 零拷贝 : Transfer memory pages between two processes with vmsplice