每当我运行我的代码时,变量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;
}
答案 0 :(得分:3)
快速搜索显示3435973836是Windows在x64上提供未分配内存的值 - 这是有道理的,因为它表明你有指向无处的指针。
让我们看看这里发生了什么。在main()
中,您定义了一堆整数。当您说inputData(&inputs, &booknmbr, &nmbrpurchased, &bookcost)
时,您会将引用传递给这些整数。该函数的签名为void inputData(int* inputs, int* booknmbr, int* nmbrpurchased, int* bookcost)
- 到目前为止一直很好。
...然后你scanf("%d", &booknmbr)
。想想这一秒。
没错。您将scanf()
引用指向要填充的整数的指针,而不仅仅是指针。这是一个容易陷入的陷阱,因为你可能总是以scanf()
的方式拨打这样的电话&amp;它变成了习惯。用scanf("%d", booknmbr)
取代它会让世界再次成为现实。
答案 1 :(得分:2)
您将变量的地址传递给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);
}
total
中的outputInfo
存在类似问题。您需要仔细阅读代码并注意细节,注意哪些变量是指针,哪些是标量值。正如我在这里所做的那样,以不同的方式命名指针是有帮助的。