[英]writing to strings inside a structure doubly linked list in C
我的代碼有問題,我更改了一些函數以適應我添加的結構,因此不是到處都是變量,但現在根本不起作用。 我需要它來創建一個結構體人員,然后提示用戶輸入人員名稱和年齡。 然后它要求更多人填寫雙向鏈接列表,如果沒有輸入任何人名,則該循環將停止循環。 然后,它吐出與我輸入的雙向鏈表相反的內容。 感謝所有幫助^-^
struct person
{
char name[10][41];
int age[10];
};
int write(struct person *people);
void print(struct person *people);
int main(void)
{
char names[10][41];
int n = 10;
int ages[10];
typedef struct person people;
n = write(people);
print(people);
system("PAUSE");
return 0;
}
int write(struct person *people)
{
int i;
char name[41];
int age[10];
for(i=0; i<=i; i++)
{
fflush(stdin);
printf("Enter full name\n");
gets(people.name);
strcpy(names[i], name);
if(names[i][0] == '\0')
break;
printf("Enter their age\n");
scanf("%d", &age[i]);
ages[i] = age[i];
}
}
void print(struct person *people)
{
int i = 0;
for(i = 0; i < 10; i++)
{
if(names[i][0] == '\0')
break;
printf("%s is %d year(s) old\n", names[i], ages[i]);
}
return i;
}
您傳遞的是剛定義的類型的名稱,而不是該類型聲明的變量,
typedef struct person people;
應該
struct person people;
更改
n = write(people);
至
n = write(&people);
刪除fflush(stdin)
是未定義的行為。
不要使用gets()
非常不安全,請改用fgets()
char name[40]; gets(name);
應該
char name[40]; fgets(name, sizeof(name), stdin);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.