[英]C programming printing a linked list, not moving along list and crashing
我正在嘗試打印一個鏈表,它的行為很奇怪,假設要移動到下一個項目,它將不會,例如,打印的第一個項目是“ Granny Smith”,然后其余的在結構中(當它進入時)到下一行,應該是新食物的是“蘭尼·史密斯”(Ranny Smith),始終沒有G,其他大多數細節都已損壞或亂碼。 任何幫助將不勝感激。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define cls system("cls");
#define flush fflush(stdin)
#define pause system("pause");
struct food // Declaration of struct
{
char produce[20];
char type[20];
char soldBy[20];
float price;
int quantityInStock;
struct food *next;
};
void addPres(struct food** tip, char tokenPro[20], char tokenType[20], char tokenSold[20], float tokenFloat, int tokenInt);
void display ();
void displayMenu ();
int getChoice ();
void readDataIn();
int main()
{
int choice = 0;
int ;
struct food first;
char *saveFloat, *saveInt;
char tokenPro[20], tokenType[20], tokenSold[20], savePro[20], saveType[20], saveSold[20], j[1900];
float tokenFloat, messFloat;
int danint, tokenInt;
struct food* a = NULL;
FILE* file;
a = NULL; // sets the first item on the list to empty
//readDataIn();
do
{
choice = getChoice();//function to present choices and receive an answer
switch (choice)//switch routes the choice
{
case 1: //to enter a new node on the list
cls;
printf("Enter the produce type: ");
fgets(tokenPro, 20, stdin);
printf("Enter the type: ");
fgets(tokenType, 20, stdin);
printf("Enter the units of measurement: ");
fgets(tokenSold, 20, stdin);
printf("\nEnter the price per %s: ", &tokenSold);
scanf("%f", &tokenFloat );
printf("\nEnter the quantity in stock: ");
scanf("%i", &tokenInt );
//pause;
addPres(&a, tokenPro, tokenType, tokenSold, tokenFloat, tokenInt);
break;
case 2: //to display the linked list
display(a);
break;
case 3: //to reverse the order
if((file=fopen("AssignmentTwoInput.txt", "r")) != NULL) //opens files
{
while(fgets(j, sizeof(j), file)) //while information is being sent to j from the file
{
strcpy(savePro, strtok(j, ","));
strcpy(saveType, strtok(NULL, ","));
strcpy(saveSold, strtok(NULL, ","));
saveFloat = strtok(NULL, ",");
saveInt = strtok(NULL, "\n");
messFloat = atof(saveFloat); //ID number is assigned to token2
danint = atoi(saveInt); //converts the value token2 points to into an int (previously read in as an string)
addPres(&a, savePro, saveType, saveSold, messFloat, danint); //sends the name and ID to the function to add a person
//printf("\n %s \t %s \t %s \t %f \t %i", savePro, saveType, saveSold, messFloat, danint);
//pause;
}//end while loop
}//end if loop
printf("\nSuccessfully loaded input file\n\n\n"); //message to tell user the back was found and imported successfully
pause;
fclose(file);
break;
case 4: //to export data
break;
case 5: //option to exit
exit(0);
break;
default://error checking
printf("Please enter a valid choice.\n");
break;
}//switch loop close
}
while (choice != 5); //end menu choices
return 0;
}//end main
void addPres(struct food** tip, char tokenPro[20], char tokenType[20], char tokenSold[20], float tokenFloat, int tokenInt)
{
struct food* temp =
(struct food*) malloc(sizeof(struct food));
strcpy(temp->produce, tokenPro);
strcpy(temp->type, tokenType);
strcpy(temp->soldBy, tokenSold);
temp->price = tokenFloat;
temp->quantityInStock = tokenInt;
//printf("\n %s \t %s \t %s \t %f \t %i", temp->produce, temp->type, temp->soldBy, temp->price, temp->quantityInStock);
//pause;
if (tip == NULL) //if the tip node is empty
{
tip = temp; //tip node becomes this node
(*tip)->next = NULL; // since there are no others yet the next node will be empty
}
else
{
temp->next = tip; // reassigns the tip node to the next one
(*tip) = temp; //inserts current node as the tip
}
}
void display(struct food* c)
{
//c = first; //assigns c to the first node on the linked list
int count = 1;
struct food *temp;
temp = c;
cls;
printf("==========================================================================\n"); //prints a header to say which info is which
printf(" Item # Produce Type Sold By Price In Stock\n");
printf("==========================================================================\n");
while( c != NULL)
{
printf("%3i \t", count);
printf("%s \t", c->produce ); //prints name
printf("%s \t", c->type ); //prints name
printf("%s \t", c->soldBy); //prints name
printf("%-3.2f \t", c->price); //prints ID
printf("%i\n", c->quantityInStock); //prints ID
pause;
count = count + 1;
c = c->next; //advances to next item in the linked list
if(c == NULL)
{
printf("\n end \n"); //once end is found user is told
pause;
}
}
printf("\n done \n");
pause;
}//end display
您有三個重要的錯誤,其中之一似乎是由於復制和粘貼某些代碼而引起的,
此printf()
語句將導致未定義的行為
printf("\\nEnter the price per %s: ", &tokenSold);
因為您傳遞的是正確的地址,但指針類型錯誤,所以指針算術將導致printf()
內部發生未定義的行為
printf("\\nEnter the price per %s: ", tokenSold); /* remove the & --------------------^ here */
您向addPres
傳遞了一個雙指針,但是您從未取消對其的引用以訪問實際的poitner,這是您要修改的,這會導致未定義的行為和觀察到的空列表的影響,因為一旦函數返回,它就不會更改原始poitner,則應使用此函數內的dereference運算符訪問double指針,例如,
if (tip == NULL)
它一定要是
if (*tip == NULL)
對於此函數內部的每次訪問也是如此。
我怎么這么快發現所有這些問題?
因為我在啟用編譯器警告的情況下編譯了程序,所以它開始顯示這些問題。
您的代碼中還會有其他問題嗎?
可能是的,但是您首先需要解決這些問題。
您fflush(stdin)
,這是一個非常常見的錯誤,似乎來自某些教程,書籍或在線資源,該標准明確指出fflush()
用於輸出流,而行為未定義用於輸入流,以下內容摘自C11草案
7.21.5.2 fflush功能
- 如果流指向未輸入最新操作的輸出流或更新流,則
fflush
函數會使該流的所有未寫入數據都被傳遞到主機環境中,並寫入文件中; 否則,行為是不確定的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.