我正在嘗試創建電話簿,但我的 insert_beg 函式出現問題。該函式的名稱本身解釋了它應該做什么。當我使用 create_pd 創建記錄時,它可以作業,然后我使用顯示功能,然后它顯示創建的記錄。然后當我嘗試使用 insert_beg 函式并輸入我的號碼和姓名時。當我嘗試使用顯示功能時,它會顯示垃圾值。預先感謝我真的很感激任何形式的幫助。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct phonedir
{
char* name;
char* phonenum;
struct phonedir *next;
};
struct phonedir *start = NULL;
void display();
struct phonedir *create_pd(struct phonedir *);
struct phonedir *insert_beg(struct phonedir *);
int main ()
{
int option;
while (option != 4)
{
printf("\n\n *****MAIN MENU *****");
printf("\n 1: Create a record");
printf("\n 2: Display the records");
printf("\n 3: insert a new record");
printf("\n 4: EXIT");
printf("\n\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1: start = create_pd(start);
printf("\n PHONE RECORD CREATED");
break;
case 2: display(start);
break;
case 3: start = insert_beg (start);
printf("PHONE RECORD ADDED \n");
break;
}
}
return 0;
}
void display()
{
struct phonedir *ptr;
ptr = start;
if(ptr != NULL)
{
printf("\t %s\n", &ptr -> phonenum);
printf("\t %s", &ptr -> name);
ptr = ptr -> next;
}
else
{
printf("Please create an entry\n");
}
}
struct phonedir *create_pd(struct phonedir *start)
{
struct phonedir *new_phonedir, *ptr;
new_phonedir = (struct phonedir *)malloc(sizeof(struct phonedir));
new_phonedir->phonenum = (char *)malloc(11*sizeof(char));
new_phonedir->name = (char *)malloc(15*sizeof(char));
printf("Enter the phone number: \n");
scanf("%s", &new_phonedir->phonenum);
printf("Enter name: \n");
scanf("%s", &new_phonedir->name);
if (start == NULL)
{
new_phonedir->next= NULL;
start = new_phonedir;
}
else
{
ptr = start;
while(ptr->next != NULL)
{
ptr = ptr->next;
}
ptr->next = new_phonedir;
new_phonedir->next = NULL;
}
return start;
}
struct phonedir *insert_beg(struct phonedir *start)
{
struct phonedir *new_phonedir;
new_phonedir = (struct phonedir *)malloc(sizeof(struct phonedir));
new_phonedir->phonenum = (char *)malloc(11*sizeof(char));
new_phonedir->name = (char *)malloc(15*sizeof(char));
printf("Enter phone number: \n");
scanf("%s", new_phonedir->phonenum);
printf("Enter name: \n");
scanf("%s", new_phonedir->name);
new_phonedir ->next = start;
start = new_phonedir;
return start;
}
uj5u.com熱心網友回復:
你的第一個問題是一個普遍的問題:當你編譯一個 C 程式時,你應該總是啟用編譯器警告。如果你這樣做了,你的編譯器會警告你將錯誤型別的引數傳遞給printf和scanf。
在四個地方,您將地址傳遞給字串而不是字串(第一個字符的地址),所以&ptr->phonenum應該是ptr->phonenum等等。
順便問一下,如果用戶輸入超過 10 個字符的電話號碼或超過 14 個字符的姓名,您認為會發生什么?
uj5u.com熱心網友回復:
scanf函式內的這些呼叫create_pd不正確并呼叫未定義的行為
printf("Enter the phone number: \n");
scanf("%s", &new_phonedir->phonenum);
printf("Enter name: \n");
scanf("%s", &new_phonedir->name);
你必須寫
printf("Enter the phone number: \n");
scanf("%s", new_phonedir->phonenum);
printf("Enter name: \n");
scanf("%s", new_phonedir->name);
寫出來會更好
printf("Enter the phone number: \n");
scanf("s", new_phonedir->phonenum);
printf("Enter name: \n");
scanf("s", new_phonedir->name);
這些 printf 呼叫也不正確
printf("\t %s\n", &ptr -> phonenum);
printf("\t %s", &ptr -> name);
你必須寫
printf("\t %s\n", ptr -> phonenum);
printf("\t %s", ptr -> name);
uj5u.com熱心網友回復:
您的printf("\t %s\n", &ptr -> phonenum);和scanf("%s", &new_phonedir->phonenum);呼叫(對于電話號碼和姓名)有一個額外的&,這是錯誤的,因為它獲取指向您的 char 陣列的指標的存盤位置。如果您輸入超過 8 個位元組(在 64 位作業系統上),這將scanf寫入指標值的存盤位置,而不是指向的位置,即指標被損壞,以及之后的資料。修復此問題后,當您輸入超過 11/15 個字符時,您仍然會遇到問題,但我想這只是練習代碼。您可能還想在您的函式中添加一個while-loop 。display():-)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/462098.html
上一篇:GoogleWebAuthorizationBroker在XamarinAndroid上不起作用
下一篇:關于fread功能的說明
