2017-10-10 10 views
-1

何が起こるかは、多量栄養素(炭水化物、脂肪およびタンパク質)の量をカロリー密度で乗算することです。ポインターは最後のポインター呼び出しで置き換えられますか?

しかし、私は結果を合計しようとすると、コードは何らかの理由で代わりに前のすべてのポインタを最後のポインタ(prot_ptr)で置き換えます。

これはソースコードである:

#include <stdio.h> 
#include <stdlib.h> 

#define CARB_CAL 4 
#define PROT_CAL 4 
#define FAT_CAL 9 

int *fat_ptr; 
int *prot_ptr; 
int *carb_ptr; 

void ask_name(void); 
void ask_fat(void); 
void ask_carb(void); 
void ask_prot(void); 

// main function 
int main (int argc, char *argv[]) 
{ 
    // food name 
    ask_name(); 
    ask_fat(); 
    ask_carb(); 
    ask_prot(); 

    int sum = *fat_ptr + *carb_ptr + *prot_ptr; 
    printf("total calories: %d\n", sum); 
} 

void ask_name(void) 
{ 
    char *name; 
    printf("input the name of the meal item: "); 
    name = malloc(6 * sizeof(char)); 
    scanf("%s", name); 
    printf("\nMEAL NAME: %s\n", name); 
    free(name); 
} 

// fats 
void ask_fat(void) 
{ 
    int fat; 
    printf("\ninput the quantity of fat: "); 
    scanf("%d", &fat); 
    printf("\n"); 
    printf("fat calories: %d\n", (fat * FAT_CAL)); 

    int int_fat = (fat * FAT_CAL); 
    fat_ptr = &int_fat; 
} 

// carbs 
void ask_carb(void) 
{ 
    int carb; 
    printf("\ninput the quantity of carbs: "); 
    scanf("%d", &carb); 
    printf("\n"); 
    printf("carb calories: %d\n", (carb * CARB_CAL)); 

    int int_carb = (carb * CARB_CAL); 
    carb_ptr = &int_carb; 
} 

// proteins 
void ask_prot(void) 
{ 
    int prot; 
    printf("\ninput the quantity of protein: "); 
    scanf("%d", &prot); 
    printf("\n"); 
    printf("protein calories: %d\n", (prot * PROT_CAL)); 

    int int_prot = (prot * PROT_CAL); 
    prot_ptr = &int_prot; 
} 

、これは出力例である:

./learn入力食アイテムの名称:米

MEAL名:米

入力脂肪量:1

脂肪カロリー:9

入力炭水化物の量:12

炭水化物のカロリー:48

入力タンパク質の量:3個の

タンパク質カロリー:12個の

総カロリー。 36

所望の結果は、合計9 + 48 +12のようになりますが、プログラムの機能は12 + 12 + 12の合計です。

+2

一時変数のアドレスが返されないため、未定義の動作が発生します。グローバル変数をポインタ以外のものにすると、コードが正常に動作するはずです。 – vasek

答えて

1

一時変数のアドレスをグローバルポインタにコピーしています。これは、ポインタが後でアクセスされるとき、未定義の動作を引き起こします。あなたの関数は一つの値だけを返すので、あなたは合計として関数の戻り値を使用して合計を計算検討することができ

int ask_fat(void) 
{ 
    int fat; 
    printf("\ninput the quantity of fat: "); 
    scanf("%d", &fat); 

    fat = fat * FAT_CAL; 
    printf("\nfat calories: %d\n", fat); 

    return fat; 
} 

は、あなたの関数の残りの部分も同じ操作を行い、その後で要約を計算しますmainとしては、以下:

int main (int argc, char *argv[]) 
{ 
    ask_name(); 

    int sum = ask_fat() + ask_carb() + ask_prot(); 
    printf("total calories: %d\n", sum); 
} 

それとも、本当にグローバル変数を使用する必要がある場合は、単に非ポインタにそれらを変更し、それぞれのask_機能に値を保存します。しかし、この場合グローバル変数は必要ではないので、避けるほうがよいでしょう。

+0

'printf(" \ nfatカロリー:%d \ n "、fat);' 1つしか必要ないときに2つのprintfを使うのはなぜですか? (大きな変化はありません) –

+0

@NicolasGuerin確かに、ありがとう! – vasek

1

ポインタで作業する必要がありますか?ポインタが問題の原因です。私はポインタのアドレスを操作しintを返す関数を使用することになります。ここでは、あなたがやっていることのために働くコードがあります。あなたが本当にポインタを使用する必要がある場合、私は一時的な変数のアドレスを取ることなくそれを編集します。

#include <stdio.h> 
#include <stdlib.h> 

#define CARB_CAL 4 
#define PROT_CAL 4 
#define FAT_CAL 9 

int fat; 
int prot; 
int carb; 

void ask_name(void); 
int ask_fat(void); 
int ask_carb(void); 
int ask_prot(void); 

// main function                                                               
int main (int argc, char **argv) 
{ 
    // food name                                                               
    ask_name(); 
    fat = ask_fat(); 
    carb = ask_carb(); 
    prot = ask_prot(); 

    int sum = fat + carb + prot; 
    printf("total calories: %d\n", sum); 
} 

void ask_name(void) 
{ 
    char *name; 
    printf("input the name of the meal item: "); 
    name = malloc(6 * sizeof(char)); 
    scanf("%s", name); 
    printf("\nMEAL NAME: %s\n", name); 
    free(name); 
} 

// fats                                                                 
int ask_fat(void) 
{ 
    int fat; 
    printf("\ninput the quantity of fat: "); 
    scanf("%d", &fat); 
    printf("\nfat calories: %d\n", (fat * FAT_CAL)); 

    int int_fat = (fat * FAT_CAL); 
    return int_fat; 
} 

// carbs                                                                 
int ask_carb(void) 
{ 
    int carb; 
    printf("\ninput the quantity of carbs: "); 
    scanf("%d", &carb); 
    printf("\ncarb calories: %d\n", (carb * CARB_CAL)); 

    int int_carb = (carb * CARB_CAL); 
    return int_carb; 
} 
関連する問題