UndefinedBehaviorSanitizer 因为空指针

UndefinedBehaviorSanitizer becouse of nullpointer

嘿,我有一些 C 代码,可以正常工作。我只是想从 typedef potatoes 获取价格到我的结账功能。我“//”未使用的代码。我需要更多信息请问,虽然我不知道如何解决这个问题。因此,非常感谢任何帮助。

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

我收到错误消息:

hj4.c:181:28: runtime error: member access within null pointer of type 'struct Product'
hj4.c:181:28: runtime error: load of null pointer of type 'float'
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1476==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000422814 bp 0x7ffd19c2f280 sp 0x7ffd19c2f260 T1476)
==1476==The signal is caused by a READ memory access.
==1476==Hint: address points to the zero page.
    #0 0x422813  (/root/sandbox/hj4+0x422813)
    #1 0x42246e  (/root/sandbox/hj4+0x42246e)
    #2 0x42232a  (/root/sandbox/hj4+0x42232a)
    #3 0x7f11cd86fb96  (/lib/x86_64-linux-gnu/libc.so.6+0x21b96)
    #4 0x402ae9  (/root/sandbox/hj4+0x402ae9)

UndefinedBehaviorSanitizer can not provide additional info.
==1476==ABORTING

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

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

编辑:编译器没有发出警告的原因是因为在顶部声明函数只用开+闭括号意味着参数列表是未定义的(为了与旧版本的 C 兼容):如果一个函数真的没有参数, 将它们定义为 int myfunction(void) 其中 void 表示 "I take no parameters"

解决这个问题

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

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

谢谢大家,我尝试了史蒂夫的代码,现在我遇到了一个我不明白的编译问题。 代码的唯一变化是:

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;

但是同样的问题来了