Is there a way to call a function expecting a pointer without creating variable?

前端 未结 2 1386
别跟我提以往
别跟我提以往 2021-01-19 14:56

I have this function call:

uint32_t func(uint32_t* a, uint32_t b)

I want to call it with an integer literal like this:

func         


        
2条回答
  •  轮回少年
    2021-01-19 15:32

    I am assuming you want to pass a pointer to an integer 0, and not the 0 (NULL) pointer.

    If you don't mind allocating dynamic memory you can do this:

    func(new uint32_t(0), b);
    

    However, then you'd have to make sure to deallocate the memory inside the function.

    Alternatively, you can use an R-value references (c++11). Then you can use the address of the reference as the pointer inside your function.

    R-value reference syntax:

    uint32_t func(uint32_t &&a, uint32_t b);
    

提交回复
热议问题