【发布时间】:2020-10-12 03:03:08
【问题描述】:
我是 C 的新手,我正在尝试制作电话簿。其中的代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
int count = 0;
struct Telephone {
char fname[20];
char lname[20];
char countrycode[2];
char areacode[3];
char number[8];
};
void addRecord(struct Telephone *T) {
if (count != 0) {
struct Telephone *temp = (struct Telephone *)realloc(T, count + 1);
}
printf("Count: %d", count);
printf("Enter first Name: ");
getchar();
do {
gets(T[count].fname);
T[count].fname[strlen(T[count].fname)] = '\0';
if (strlen(T[count].fname) == 0)
printf("Empty Value Not Permitted! Please try again: ");
} while (strlen(T[count].fname) == 0);
printf("Enter last name: ");
gets(T[count].lname);
printf("%s", T[count].lname);
printf("Enter Country Code: ");
gets(T[count].countrycode);
printf("Enter Area Code: ");
gets(T[count].areacode);
printf("Enter Telephone Number: ");
do {
gets(T[count].number);
T[count].number[strlen(T[count].number)] = '\0';
if (strlen(T[count].number) == 0)
printf("Value Not Permitted! Please try again: ");
} while (strlen(T[count].number) == 0);
count += 1;
printf("Added\n");
}
void main() {
struct Telephone *T = (struct Telephone *)malloc(sizeof(struct Telephone));
int option = 1;
while (option != 0) {
printf("1 -> Add new record\n2 -> Search for record\n3 -> Display all record\n4 -> Update record\n0 -> Exit\nEnter: ");
scanf("%d", &option);
if (option == 1)
addRecord(T);
if (option == 3) {
for (int i = 0; i < count; i++)
printf("FN: %s LN: %s N: %s", T[i].fname, T[i].lname, T[i].number);
}
}
}
我只在开始时添加和显示方法来检查是否所有内容都正确添加。但是,当我尝试添加值时,在第一次输入后,存储的值是垃圾。我尝试添加 3 个值并打印名字、姓氏和数字。我得到的值是:
FirstName: hello LastName: └ Number: 123
FirstName: hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number: o1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number:
FirstName: ame: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number: o1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number:
LastName: LastName: : hello1 LastName: Number: o1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number:
LastName: LastName: : hello1 Number: ame: Number: o1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number:
LastName: LastName: : hello1 LastName: Number: o1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: : hello1 LastName: Number:
LastName: LastName: : hello1 Number:
作为参考,输入为:1->hello,world,123 2-> hello1,world1,456 3->hello2,world2,789
为什么要打印此垃圾值以及如何修复它。我在这里使用了动态分配的结构指针。
【问题讨论】:
-
查看非常小的字符串长度,您可能会遇到缓冲区溢出。无论如何,请阅读Why is the gets function so dangerous that it should not be used?
gets()功能已过时。 -
您在
scanf()之后也调用了gets()。请参阅scanf() leaves the newline char in the buffer。 -
我打电话给
gets(),因为我还需要能够接受空输入,从这里的帖子中我看到可以由gets()或fgets()完成,但不是scanf跨度> -
请立即停止使用
gets。使用fgets()获取所有输入并应用sscanf提取菜单选项(或查看第一个字符)。对于字符串,您需要Removing trailing newline character from fgets() input
标签: c pointers struct malloc dynamic-memory-allocation