每当我运行我的代码时,变量 bookcost 都会改变自己的值: 3435973836 。
它在到达 processingData 函数后执行此操作。我不知道为什么它一直这样做!我已经编写了这个没有函数的程序,它运行良好:( 这是我的第二节编程课,它是在线的,几乎没有教授的支持。
#include <stdio.h>
void inputData(int* inputs,int* booknmbr, int* nmbrpurchased, int* bookcost);
void processingData(int bookcost, int nmbrpurchased, int totalpurch, int costaccu);
void outputInfo(int booknmbr, int nmbrpurchased, int bookcost, int* total);
int bookcounter = 0;
int totalpurch = 0;
int costaccu = 0;
int totalcostaccu = 0;
int main ()
{
int booknmbr;
int nmbrpurchased;
int bookcost;
int bookcounter = 0;
int cycleRun;
int total;
int inputs;
printf("Run Program? 1 for Yes or -1 for No \n"); // ask user to input product name
scanf("%d", &cycleRun);
while (cycleRun != -1)
{
inputData(&inputs, &booknmbr, &nmbrpurchased, &bookcost);
processingData(bookcost, nmbrpurchased, totalpurch, costaccu);
outputInfo(booknmbr, nmbrpurchased, bookcost, &total);
printf("Run Program? 1 for Yes or -1 for No \n"); // ask user to input product name
scanf("%d", &cycleRun);
}
}
void inputData(int* inputs, int* booknmbr, int* nmbrpurchased, int* bookcost)
{
printf( "\nEnter the Book Product Number?\n" );
scanf("%d", &booknmbr);
printf( "Enter the Number of Books Purchased?\n" );
scanf("%d", &nmbrpurchased);
printf( "Enter the Cost of the Book Purchased?\n");
scanf("%d", &bookcost);
printf( "TEST: %u\n", bookcost);
return;
}
void processingData(int bookcost, int nmbrpurchased, int totalpurch, int costaccu)
{
int total;
printf( "TEST: %u\n", bookcost);
total = bookcost * nmbrpurchased;
totalpurch = totalpurch + nmbrpurchased;
costaccu = costaccu + bookcost;
totalcostaccu = totalcostaccu + (bookcost * nmbrpurchased);
printf( "TEST: %u\n", bookcost);
return;
}
void outputInfo(int booknmbr, int nmbrpurchased, int bookcost, int* total)
{
printf( "\nBook Product number entered is: %u\n", booknmbr);
printf( "Quantity of Book Purchased is: %u\n", nmbrpurchased);
printf( "Cost of the Book Purchased is: %u\n", bookcost);
printf( "Total cost of books is: $%u\n", total);
return;
}
void outputSummary(int bookcounter, int totalpurch, int costaccu, int totalcostaccu)
{
printf( "\n\nNumber of records processed = %u\n", bookcounter);
printf( "Number of books purchased = %u\n", totalpurch);
printf( "Cost of the books purchased = $%u\n", costaccu);
printf( "Total cost for all book purchases = $%u\n", totalcostaccu);
return;
}
Run Code Online (Sandbox Code Playgroud)
快速搜索显示 3435973836 是 Windows 在 x64 上提供未分配内存的值 - 这是有说服力的,因为它表明您有指向无处的指针。
让我们看看这里发生了什么。在 中main(),您定义了一堆整数。当你说inputData(&inputs, &booknmbr, &nmbrpurchased, &bookcost). 该函数的签名为void inputData(int* inputs, int* booknmbr, int* nmbrpurchased, int* bookcost)- 到目前为止一切顺利。
……那么你scanf("%d", &booknmbr)。考虑一下。
这是正确的。您正在传递scanf()对指向要填充的整数的指针的引用,而不仅仅是指针。这是一个容易掉入的陷阱,因为您可能总是以scanf()这种方式编写调用并已成为习惯。用 a 替换它scanf("%d", booknmbr)会使世界再次正确。
您将变量的地址inputData传递给...尝试以下操作:
/* not using inputs? */
void inputData(int* pinputs, int* pbooknmbr, int* pnmbrpurchased, int* pbookcost) {
printf( "\nEnter the Book Product Number?\n" );
scanf("%d", pbooknmbr);
printf( "Enter the Number of Books Purchased?\n" );
scanf("%d", pnmbrpurchased);
printf( "Enter the Cost of the Book Purchased?\n");
scanf("%d", pbookcost);
printf( "TEST: %u\n", *pbookcost);
}
Run Code Online (Sandbox Code Playgroud)
total您在 中也有类似的问题outputInfo。您需要仔细阅读代码并注意细节,注意哪些变量是指针,哪些是标量值。它有助于以不同的方式命名指针,就像我在这里所做的那样。