进程返回 -1073741819 (0xC0000005)
Posted
技术标签:
【中文标题】进程返回 -1073741819 (0xC0000005)【英文标题】:Process returned -1073741819 (0xC0000005) 【发布时间】:2014-04-28 04:41:18 【问题描述】:我正在尝试为我的 C 编程课程编写一个程序。在我以零错误和警告运行程序后,标题中的错误不断弹出。这是我的代码
/*
Write a program that contains the function calories() that is to accept a long integer number total and the addresses of the integer variables pizza, chips, apples, and mustard.
The passed long integer represents the total number of calories you are able to consume in this meal, and the function is to determine the
number of calories in slices of pizza, bags of chips, apples,
and teaspoons of mustard in the passed value, writing these values directly into the respective variables declared in the calling function.
This function will be called from the main program and when it returns to main, it will print out the values of variables pizza, chips, apples, and mustard.
The calories in our favorite foods are in this order:
pizza--385 calories each slice
chips--170 calories each bag
apple--80 calories each
mustard--5 calories each tsp.
For example, if I enter a total amount of calories of 1050, I can eat:
2 slices pizza @ 770 calories (1050 - 770 = 280 calories remain)
1 bag of chips @ 170 calories (280 - 170 = 110 calories remain)
1 apple @ 80 calories (110 - 80 = 30 calories remain)
6 tsp. mustard @ 30 calories
*/
#include <stdio.h>
int main()
int calorie(int, int , int, int);
int total, pizza, chips, apples, mustard, sum, sum1, sum2, sum3;
pizza = 385;
chips = 170;
apples = 80;
mustard = 5;
printf("How many calories do you want to consume during your meal?");
scanf("%d", total);
total=calorie(pizza, chips, apples, mustard);
printf("Pizza: %.2d", &pizza);
printf("Chips: %.2d", &sum1);
printf("Apples: %.2d", &sum2);
printf("Mustard: %.2d", &sum3);
return 0;
int calorie(pizza, chips, apples, mustard)
int clfp, sum1, sum2, clfa, clfc, sum3, calorie;
pizza = calorie/pizza;
sum1 = calorie-pizza; /*calories left from pizza*/
sum1 = clfp/chips;
clfc = clfp-chips; /*calories left from chips*/
sum2 = clfc/apples; /*calories life from apples*/
clfa = clfc-apples ;
sum3 = clfa/mustard;
return pizza, sum1, sum2, sum3;
【问题讨论】:
你不能做return pizza, sum1, sum2, sum3;
,你只能返回1个值。
@Rohan:编译器返回了sum3
的值——其他的就是对逗号运算符的求值。
@MichaelBurr,是的,但不确定 OP 打算这样做。
@Rohan - 我相信你是对的,这不是预期的。我以为你是在说编译器不允许这样做 - 我现在明白你在说什么了。
@Gizmo:尝试使用编译器选项-Wall
,它将指出程序中的许多问题。
【参考方案1】:
1) 改变
int calorie(int, int , int, int);
到
int calorie(int *, int *, int *, int *); // send pointers, so that you can update them in function call.
2) 更改调用
calorie(pizza, chips, apples, mustard);
到
calorie(&pizza, &chips, &apples, &mustard); // send address of variables.
3) 更改返回
return pizza, sum1, sum2, sum3;
到
return 0; // to indicate success.
4) 改变
printf("Pizza: %.2d", &pizza);
printf("Chips: %.2d", &sum1);
printf("Apples: %.2d", &sum2);
printf("Mustard: %.2d", &sum3);
到
printf("Pizza: %.2d", pizza);
printf("Chips: %.2d", sum1);
printf("Apples: %.2d", sum2);
printf("Mustard: %.2d", sum3);
5) 改变
scanf("%d", total);
到
scanf("%d", &total);
【讨论】:
【参考方案2】:scanf("%d", total);
应该是scanf("%d", &total);
此外,在 scanf()
调用中读取 total
的值也没有多大意义,只是为了完全忽略它并通过调用覆盖该值:
total=calorie(pizza, chips, apples, mustard);
另一个问题是您使用变量calorie
却没有在calorie()
函数中初始化它:
pizza = calorie/pizza;
// ^^^^^ uninitialized
我也很惊讶您的 calorie()
函数定义甚至可以编译,因为括号中的标识符都不是类型。但我发现它为我编译,所以我知道什么(我猜这是隐含的int
抬起丑陋的头)?
int calorie(pizza, chips, apples, mustard) // <- what?
// ....
【讨论】:
【参考方案3】:在所有这些建议之后,这里是一个固定版本的代码,供您参考:
/*
Write a program that contains the function calories() that is to accept
a long integer number total and the addresses of the integer variables
pizza, chips, apples, and mustard.
The passed long integer represents the total number of calories you are
able to consume in this meal, and the function is to determine the number
of calories in slices of pizza, bags of chips, apples, and teaspoons of
mustard in the passed value, writing these values directly into the
respective variables declared in the calling function.
This function will be called from the main program and when it returns to
main, it will print out the values of variables pizza, chips, apples, and
mustard.
The calories in our favorite foods are in this order:
pizza--385 calories each slice
chips--170 calories each bag
apple--80 calories each
mustard--5 calories each tsp.
For example, if I enter a total amount of calories of 1050, I can eat:
2 slices pizza @ 770 calories (1050 - 770 = 280 calories remain)
1 bag of chips @ 170 calories (280 - 170 = 110 calories remain)
1 apple @ 80 calories (110 - 80 = 30 calories remain)
6 tsp. mustard @ 30 calories
*/
#include <stdio.h>
void calorie(long, int *, int *, int *, int *);
int main()
long total;
int pizza, chips, apples, mustard;
printf("How many calories do you want to consume during your meal? ");
scanf("%ld", &total);
calorie(total, &pizza, &chips, &apples, &mustard);
printf("Pizza: %2d\n", pizza);
printf("Chips: %2d\n", chips);
printf("Apples: %2d\n", apples);
printf("Mustard: %2d\n", mustard);
return 0;
void calorie(long total, int *pizza, int *chips, int *apples, int *mustard)
const int per_pizza = 385;
const int per_chips = 170;
const int per_apples = 80;
const int per_mustard = 5;
*pizza = total / per_pizza;
total -= *pizza * per_pizza;
*chips = total / per_chips;
total -= *chips * per_chips;
*apples = total / per_apples;
total -= *apples * per_apples;
*mustard = total / per_mustard;
total -= *mustard * per_mustard;
【讨论】:
以上是关于进程返回 -1073741819 (0xC0000005)的主要内容,如果未能解决你的问题,请参考以下文章
进程以退出代码 -1073741515 (0xC0000135) 结束
这是啥错误,我该如何解决?进程以退出代码 -1073740791 (0xC0000409) 结束
模拟器:进程以退出代码 -1073741515 (0xC0000135) 结束