C - 垃圾字符,即使有空终止符,结构

标签 c string struct character garbage

因此,作为学校作业的一部分,我必须创建一个应用程序来使用结构处理各种字段的输入。在 main 之外定义如下

typedef struct stud {
    struct number {
        int studNum;
    } number;
    struct name {
        char firstName[NAME_SIZE];
        char lastName[NAME_SIZE];
    } name;
    struct cellNum {
        int areaCode;
        int prefix;
        int suffix;
    } cellNum;
    struct courses {
        struct fall {
            char className[NAME_SIZE];
            float mark;
        } fall;
        struct winter {
            char className[NAME_SIZE];
            float mark;
        } winter;
    } courses;
} stud;

NAME_SIZE 定义为 30

我按如下方式访问结构

stud studArray[100]; // max 100 students
stud *studPtr;

for (int i = 0; i < studCount; i++) {
    studPtr = &studArray[i];
    initializeStrings(studPtr[i]);
    getNum(studPtr[i]);
    getName(studPtr[i]);
    getCell(studPtr[i]);
    getCourses(studPtr[i]);
}

initializeStrings 是我添加空终止符的地方

void initializeStrings(stud student) {
    student.name.firstName[NAME_SIZE - 1] = '\0'; 
    student.name.lastName[NAME_SIZE -1] = '\0';
    student.courses.fall.className[NAME_SIZE -1] = '\0';
    student.courses.winter.className[NAME_SIZE -1] = '\0';
}

这就是现在出现问题的地方:在我的各种函数中的任何一个中,当我尝试访问我写入字符串的任何信息时,我得到一个看起来像横向 T 的垃圾字符输出,但前提是我以不同的功能访问数据。

示例:这是获得 2 门类(class)和分数的代码

void getCourses(stud student) {
    getchar();
    printf("\n%s", "Enter Course name - Fall 2016: ");
    fgets(student.courses.fall.className, NAME_SIZE - 1, stdin);
    fflush(stdin); // suggested by my prof to fix the issue, did nothing

    printf("\n%s%s%s", "Enter mark received for ", student.courses.fall.className, ": ");
    scanf("%f", &student.courses.fall.mark);

    getchar();
    printf("\n%s", "Enter Course name - Winter 2017: ");
    fgets(student.courses.winter.className, NAME_SIZE - 1, stdin);

    printf("\n%s%s%s", "Enter mark received for ", student.courses.winter.className, ": ");
    scanf("%f", &student.courses.winter.mark);
}

如果我输入 helloWorld 作为类(class)名称,系统会正确打印 输入 helloWorld 收到的标记

但是当我尝试从打印功能打印数据时:

void printData(stud student) {
// various other values (none of which work)
printf("\t%s\t%s\t%f\n", "Fall2016", student.courses.fall.className, student.courses.fall.mark);
}

我得到垃圾数据输出(image)

欢迎任何帮助/代码批评

编辑:谢谢 Eddiem 和其他所有人,您为我指明了正确的方向。

最佳答案

这段代码没有任何意义。首先,为什么不将 stud* 指针传递给您在下面调用的每个函数?无论如何,这里的方式是初始化 studPtr 以指向 studArray 数组中的特定元素,但随后仍在索引 i 进入那个指针。您可能会传递 studPtr[0],但这很奇怪。

原代码:

stud studArray[100]; // max 100 students
stud *studPtr;

for (int i = 0; i < studCount; i++) {
    studPtr = &studArray[i];
    initializeStrings(studPtr[i]);
    getNum(studPtr[i]);
    getName(studPtr[i]);
    getCell(studPtr[i]);
    getCourses(studPtr[i]);
}

我建议修改如下:

void initializeStrings(stud *student) {
    student->name.firstName[NAME_SIZE - 1] = '\0'; 
    student->name.lastName[NAME_SIZE -1] = '\0';
    student->courses.fall.className[NAME_SIZE -1] = '\0';
    student->courses.winter.className[NAME_SIZE -1] = '\0';
}

如果当前采用 stud 参数的函数,则需要对每个函数进行这些修改。然后,将循环初始化更改为:

for (int i = 0; i < studCount; i++) {
    initializeStrings(&studArray[i]);
    getNum(&studArray[i]);
    getName(&studArray[i]);
    getCell(&studArray[i]);
    getCourses(&studArray[i]);
}

关于C - 垃圾字符,即使有空终止符,结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40896513/

相关文章:

c++ - C++访问结构中的结构数组的元素

c++ - 奇怪的 C++ 模板问题

python - python中的空字符串?

c - 简单的多线程程序段错误

java - 如何在java中将camelCase字符串转换为下划线,保留一些大写,其余的小写?

python - 未知的 python 表达式 filename=r'/path/to/file'

go - 在函数中返回结构的值并在另一个包中使用它

c++ - 是否有 QPair 类,但用于三个以上的项目而不是两个?

c++ - 如何使用 write() 函数将结构写入文件?

二进制数从 ASCII 到十进制的转换