C 为什么我在显示数据后得到垃圾值

C 为什么我在显示数据后得到垃圾值,c,arrays,database,structure,C,Arrays,Database,Structure,当我显示记录时,我得到了垃圾值 我必须用C语言创建一个学生数据库,使用结构数组,不使用指针 还有别的方法吗 如何使用结构数组 #include <stdio.h> struct student { char first_name[10],last_name[10]; int roll; char address[20]; float marks; }; void accept(struct student); void display(struct

当我显示记录时,我得到了垃圾值

我必须用C语言创建一个学生数据库,使用结构数组,不使用指针

还有别的方法吗

如何使用结构数组

#include <stdio.h>

struct student {
    char first_name[10],last_name[10];
    int roll;
    char address[20];
    float marks;
};

void accept(struct student);
void display(struct student);

void main() {
    struct student S[10];
    int n, i;
    printf("Enter the number of records to enter : ");
    scanf("%d", &n);

    for (i = 0; i < n; i++) {
        accept(S[i]);
    }

    for (i = 0; i < n; i++) {
        display(S[i]);
    }
}

void accept(struct student S) {
    scanf("%s", S.first_name);
    scanf("%s", S.last_name);
    scanf("%d", &S.roll);
    scanf("%s", S.address);
    scanf("%f", &S.marks);
}

void display(struct student S) {
    printf("\n%s", S.first_name);
    printf("\n%s", S.last_name);
    printf("\n%d", S.roll);
    printf("\n%s", S.address);
}
C中的所有内容都是按值传递的。这意味着在堆栈帧中修改变量副本,而作为参数传递的实变量保持不变

您必须传递一个指向要在函数中修改的变量的指针

// Function declaration
void accept(struct student *);

// Call
accept(&S[i]);

// Usage in function via dereference operator
scanf("%s",S->first_name);
如果要输入未知数量的记录,则应使用或动态分配结构

弗拉

动态呼叫


因为在您的情况下,如果用户输入的记录超过9条,您所接触的记录超出了边界,这具有未定义的行为。

您的代码中存在多个问题:

没有参数的main的标准原型是int mainvoid 您应该使用calloc动态分配数组。 应该将结构指针传递给accept和display函数,而不是按值传递结构。按值传递目标结构是不正确的,因为accept函数无法修改主函数中的结构,这将保持未初始化状态并导致显示垃圾。请注意,访问未初始化的数据实际上是未定义的行为,因此程序的行为可能更糟糕。 您应该为scanf提供最大数量的参数以存储到字符数组中,以避免潜在的缓冲区溢出。 您应该验证scanf的返回值,以避免在无效输入上出现未定义的行为。 您可以使用%[^\n]扫描集在地址字段中允许嵌入空格。 以下是修改后的版本:

#include <stdio.h>
#include <stdlib.h>

struct student {
    char first_name[10], last_name[10];
    int roll;
    char address[20];
    float marks;
};

void accept(struct student *sp);
void display(const struct student *sp);

int main(void) {
    struct student *S;
    int n, i, j;
    printf("Enter the number of records to enter : ");
    if (scanf("%d", &n) != 1)
        return 1;

    S = calloc(sizeof(*S), n);
    if (S == NULL) {
        return 1;
    }
    for (i = 0; i < n; i++) {
        accept(&S[i]);
    }
    for (i = 0; i < n; i++) {
        display(&S[i]);
    }
    free(S);
    return 0;
}

void accept(struct student *sp) {
    if (scanf("%9s%9s&d %19[^\n]%f",
        sp->first_name, sp->last_name, &sp->roll,
        sp->address, &sp->marks) != 5) {
        printf("missing input\n");
        exit(1);
    }
}

void display(const struct student *sp) {
    printf("%s\n", sp->first_name);
    printf("%s\n", sp->last_name);
    printf("%d\n", sp->roll);
    printf("%s\n", sp->address);
    printf("%f\n", sp->marks);
    printf("\n");
}

C11标准草案n1570:6.5.2.2函数调用4参数可以是任何完整对象类型的表达式。在准备调用函数时,将计算参数,并为每个参数指定相应参数的值。93A函数可能会更改其参数的值,但这些更改不会影响参数的值[…]因此,void acceptstruct student*S{scanf%S,S->first_name;。调用accept&S[i];非常感谢好友。非常感谢好友。void displaystruct*student;display&S[i];->void acceptstruct student*;接受&S[i];叹气..void acceptstruct*student;:这是语法错误。天哪,我应该多加注意
scanf("%d",&n);
struct student * S = malloc(sizeof(struct student) * n);
#include <stdio.h>
#include <stdlib.h>

struct student {
    char first_name[10], last_name[10];
    int roll;
    char address[20];
    float marks;
};

void accept(struct student *sp);
void display(const struct student *sp);

int main(void) {
    struct student *S;
    int n, i, j;
    printf("Enter the number of records to enter : ");
    if (scanf("%d", &n) != 1)
        return 1;

    S = calloc(sizeof(*S), n);
    if (S == NULL) {
        return 1;
    }
    for (i = 0; i < n; i++) {
        accept(&S[i]);
    }
    for (i = 0; i < n; i++) {
        display(&S[i]);
    }
    free(S);
    return 0;
}

void accept(struct student *sp) {
    if (scanf("%9s%9s&d %19[^\n]%f",
        sp->first_name, sp->last_name, &sp->roll,
        sp->address, &sp->marks) != 5) {
        printf("missing input\n");
        exit(1);
    }
}

void display(const struct student *sp) {
    printf("%s\n", sp->first_name);
    printf("%s\n", sp->last_name);
    printf("%d\n", sp->roll);
    printf("%s\n", sp->address);
    printf("%f\n", sp->marks);
    printf("\n");
}