Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this statement mean in C: "(void)ptr;" [duplicate]

Tags:

c

embedded

stm32

It may not be clear from the title. I came across the following code in an embedded STM32 project. I don't understand the line inside the function.

    static void txend1(UARTDriver *uartp) {
        (void)uartp; // what does this do? Is it a statement?
    }

I've tried searching elsewhere online, but most results are casting pointers to void pointers, which I don't think this is. Thanks for the help!

like image 514
TowerFan Avatar asked Sep 20 '17 22:09

TowerFan


People also ask

What is void * p in C?

The void pointer in C is a pointer that is not associated with any data types. It points to some data location in the storage. This means that it points to the address of variables. It is also called the general purpose pointer. In C, malloc() and calloc() functions return void * or generic pointers.

What does void * p mean?

void *p( p is a void pointer) is a pointer which we can be explicitly cast to any data type. These pointers show difference only during dereferencing of pointers.

What is void argc?

(void)argc; (void)argv; If a function argument is not used, like in your program, then this is the idiomatic way of suppressing the warning of unused function argument issued by some compilers. Any decent compiler will not generate code with these statements.

Can you free a void * in C?

Description. The C library function void free(void *ptr) deallocates the memory previously allocated by a call to calloc, malloc, or realloc.


1 Answers

this is just a portable way to suppress the warning on this unused uart parameter.

It has no effect, but compilers see that as used, and don't issue any warning.

Very useful when the prototype of the function is imposed / cannot be changed (callback function) but your implementation doesn't need this parameter.

(note that gcc favors the __attribute__((unused)) construct, easier to understand, but not compatible with all compilers)

like image 195
Jean-François Fabre Avatar answered Oct 17 '22 15:10

Jean-François Fabre