进程返回-1073741819(0xC0000005)

时间:2014-04-28 04:41:19

标签: c windows

我正在尝试为我的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;
}

3 个答案:

答案 0 :(得分:3)

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);

答案 1 :(得分:2)

scanf("%d", total);应为scanf("%d", &total);

total调用中读取scanf()的值只是为了完全忽略它并通过调用覆盖该值,这没有多大意义:

total=calorie(pizza, chips, apples, mustard);

另一个问题是你使用变量calorie而没有在calorie()函数中初始化它:

pizza = calorie/pizza;
//       ^^^^^ uninitialized

我还惊讶于你的calorie()函数定义甚至编译,因为parens中的所有标识符都不是类型。但是我发现它为我编译,所以我知道什么(我猜它隐含int它的丑陋头脑?)

int calorie(pizza, chips, apples, mustard)  // <- what?
{
    // ....
}

答案 2 :(得分:0)

完成所有这些建议之后,这里是代码的固定版本,供您参考:

/*
   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;
}