【发布时间】:2017-08-26 13:03:18
【问题描述】:
我在显示记录时得到垃圾值。
我必须用 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); }
【问题讨论】:
-
C11 标准草案 n1570:6.5.2.2 函数调用 4 参数可以是任何完整对象类型的表达式。在准备调用函数时,会评估参数,并为每个参数分配相应参数的值。 93)一个函数可以改变其参数的值,但这些改变不会影响参数的值[...]
-
所以,
void accept(struct student *S){ scanf("%s", S->first_name);.. 打电话给accept(&S[i]); -
非常感谢朋友。
标签: c arrays database structure