C 由于空指针,未定义的行为管理器

C 由于空指针,未定义的行为管理器,c,typedef,null-pointer,C,Typedef,Null Pointer,嘿,我有一些C语言的代码,可以用。我只是想从我的退房功能中得到一个价格。I“/”未使用的代码。我需要更多的信息,尽管我不知道如何解决这个问题,但请询问。因此,非常感谢您的帮助 #include <stdio.h> #include <string.h> int number; void get_number ( ); void show_menu ( ); int get_input ( ); void chech_o

嘿,我有一些C语言的代码,可以用。我只是想从我的退房功能中得到一个价格。I“/”未使用的代码。我需要更多的信息,尽管我不知道如何解决这个问题,但请询问。因此,非常感谢您的帮助

 #include <stdio.h>
 #include <string.h>   

    int number;

    void get_number ( );
    void show_menu ( );
    int get_input ( );
    void chech_out ();

    struct Product 
    {
            float potatoes;
    };
    typedef struct Product product;

    int main (void)
    {
        struct Product product;

        while (1)
        {
            //show_product1 ();
            show_menu ();
            if (get_input ()) // end on "a"
                break;
        }
        printf ("Bye!");
        return 0;
    }



    void show_menu (void)
    {
        printf ("Type what you wanne do");
        printf ("f)change amount    h)chech out    s)Strawberry");
        printf ("c)carrots    p)potatoes  o)Onion");
        printf ("a)quit");
    }

    void get_number ( )
    {

        printf ("Enter number in kg: ");
        scanf ("%d", &number);
    }



    int get_input (struct Product *product )
    {
        char letter;

        scanf ("%c", &letter);
        switch (letter)
        {
            case 'f':
                printf("\n\n\nWhenever you enter a new amount it will reset the old one!\n\n");

                break;
            case 'h':
                chech_out();


                break;
            case 'c':

                break;
            case 'p':


                get_number();           
                float P_freightPrice = number*12;
                float P_kgPrice = number*25;
                float P_totalprice=P_freightPrice+P_kgPrice;
                product->potatoes = P_totalprice;
           printf("\n%f\n",P_totalprice);     

         if (P_totalprice <= 100)
         {
           printf("%f\n",P_totalprice);    
         }
         if (P_totalprice>=101 && number<=350)
         {
             float New=(P_totalprice/100)*5;
             float total=P_totalprice-New;
             printf("%f\n",total);    
         }
         if(P_totalprice>=351 && P_totalprice<=600)
         {
             float New=(P_totalprice/100)*10;
             float total=P_totalprice-New;
             printf("%f\n",total); 

         }
        if(P_totalprice>=601)
        {
             float New=(P_totalprice/100)*15;
             float total=P_totalprice-New;
             printf("%f\n",total);    
        }  

                break;
            case 'a':
                return 1;
            default:
                ;
        }
        return 0;
    }

    void chech_out (struct Product *product)
    {
        printf("%2.f",product->potatoes);
    }

我想我只是需要一些指针来解决这个问题;)

我不知道为什么编译器没有对此发出警告,但是您的
chech_out
函数在顶部声明为
chech_out()
-没有参数-但是定义包括一个:
chech_out(struct Product*Product)
。调用该函数时,它将堆栈上的垃圾作为指向产品的指针处理,但失败了

编辑:编译器没有发出警告的原因是,只使用开括号+闭括号在顶部声明函数意味着参数列表未定义(与旧版本的C兼容):如果函数确实不接受任何参数,则将其定义为
int myfunction(void)
,其中
void
表示“我不接受任何参数”

要解决这个问题

// define struct Product here
void get_number (void);
void show_menu (void);
int get_input (struct Product *);
void chech_out (struct Product *);

现在让你的编译器告诉你你错过了什么。

我不知道为什么你的编译器没有对此发出警告,但是你的
chech_out
函数被声明为
chech_out()
-没有参数-但是定义包括一个:
chech_out(struct Product*Product)
。调用该函数时,它将堆栈上的垃圾作为指向产品的指针处理,但失败了

编辑:编译器没有发出警告的原因是,只使用开括号+闭括号在顶部声明函数意味着参数列表未定义(与旧版本的C兼容):如果函数确实不接受任何参数,则将其定义为
int myfunction(void)
,其中
void
表示“我不接受任何参数”

要解决这个问题

// define struct Product here
void get_number (void);
void show_menu (void);
int get_input (struct Product *);
void chech_out (struct Product *);

现在让你的编译器告诉你你错过了什么。

谢谢大家,我尝试了Steve的代码,现在我遇到了一个编译问题,我不明白。 代码中唯一的更改是:

void get_number (void);
void show_menu (void);
int get_input (struct Product *);
void chech_out (struct Product *);
int number;
我发现并得到错误:

hj4.c:35:18: error: too few arguments to function call, expected 1, have 0
                if (get_input ()) // end on "a"
                    ~~~~~~~~~  ^
hj4.c:21:1: note: 'get_input' declared here
int get_input (struct Product *);
^
hj4.c:73:14: error: too few arguments to function call, expected 1, have 0
                        chech_out();
                        ~~~~~~~~~ ^
hj4.c:22:1: note: 'chech_out' declared here
void chech_out (struct Product *);
^
2 errors generated.
我试着把代码改成

void get_number (void);
void show_menu (void);
int get_input (struct Product *product);
void chech_out (struct Product *product);
int number;

但是同样的问题出现了。

谢谢大家,我尝试了Steve的代码,现在我遇到了一个编译问题,我不明白。 代码中唯一的更改是:

void get_number (void);
void show_menu (void);
int get_input (struct Product *);
void chech_out (struct Product *);
int number;
我发现并得到错误:

hj4.c:35:18: error: too few arguments to function call, expected 1, have 0
                if (get_input ()) // end on "a"
                    ~~~~~~~~~  ^
hj4.c:21:1: note: 'get_input' declared here
int get_input (struct Product *);
^
hj4.c:73:14: error: too few arguments to function call, expected 1, have 0
                        chech_out();
                        ~~~~~~~~~ ^
hj4.c:22:1: note: 'chech_out' declared here
void chech_out (struct Product *);
^
2 errors generated.
我试着把代码改成

void get_number (void);
void show_menu (void);
int get_input (struct Product *product);
void chech_out (struct Product *product);
int number;

但同样的问题也出现了。

使用不精确的数据类型,如
float
double
不适合保存货币值,它们可能会在计算时给您带来“奇怪”的结果。通常也不建议使用全局变量传递函数参数或返回值。它使函数非线程安全、不可重入、浪费内存,最重要的是难以读取,特别是当您使用非描述性名称(如
number
)时。编译程序时是否收到编译警告?如果没有,请使用
-Wall
打开警告。然后尝试理解并修复警告。如果您对此有任何问题,我建议您发布一个新问题,其中包括警告消息。使用不精确的数据类型,如
float
double
不适合保存货币值,它们可能会在计算时给您带来“奇怪”的结果。通常也不建议使用全局变量传递函数参数或返回值。它使函数非线程安全、不可重入、浪费内存,最重要的是难以读取,特别是当您使用非描述性名称(如
number
)时。编译程序时是否收到编译警告?如果没有,请使用
-Wall
打开警告。然后尝试理解并修复警告。如果您对此有任何问题,我建议您发布一个新问题,其中包括警告消息。编译器会告诉您问题所在。定义(您提供代码主体的地方)说它需要一个指向
产品的指针,但是当您调用函数时,您没有传递参数。对不起,金发碧眼,但我该怎么做?我确实试着像这样在函数中放置传递参数;如果(get_input(struct Product)),但不起作用我妻子是金发,所以我很同情:-)看看你的
get_input
函数,它会提示用户输入一个字符,并做很多其他事情。此函数需要
struct Product*Product
作为参数,其中调用者说“在此处传递数据”。它希望得到一个指针。但是,当您在代码前面调用函数时,您没有传递指针:您希望
get\u input
将数据传递到哪里?我猜您想使用
main()
中的
product
var调用,因此编译器会告诉您问题所在。定义(您提供代码主体的地方)说它需要一个指向
产品的指针,但是当您调用函数时,您没有传递参数。对不起,金发碧眼,但我该怎么做?我确实试着像这样在函数中放置传递参数;如果(get_input(struct Product)),但不起作用我妻子是金发,所以我很同情:-)看看你的
get_input
函数,它会提示用户输入一个字符,并做很多其他事情。此函数需要
struct Product*Product
作为参数,其中调用者说“在此处传递数据”。它希望得到一个指针。但是,当您在代码前面调用函数时,您没有传递指针:您希望
get\u input
将数据传递到哪里?我猜您希望使用
main()
中的
product
var调用,因此它应该是
get\u input(&product)