Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c - *(void **) &(int[2]){0,PAGE_SIZE}; meaning?

Tags:

c

pointers

Context

Reading some kernel code.

Problem

I cannot get my head on what this line is meaning

*(void **) &(int[2]){0,PAGE_SIZE};

and more, what does this means

{0,PAGE_SIZE}

To me it doesn't look like a function with that comma.

What could be going on with this code ? I don't understand the indirections here.

Is it a function or a cast ? What does the bracket part means ? Seems so convoluted to me but definitely has a meaning.

like image 814
Larry Avatar asked Sep 15 '14 19:09

Larry


People also ask

What is void * function in C?

Void functions are stand-alone statements In computer programming, when void is used as a function return type, it indicates that the function does not return a value. When void appears in a pointer declaration, it specifies that the pointer is universal.

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 is void * used for?

void (C++) When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword. A void* pointer can't be dereferenced unless it's cast to another type.

Can you free a void * in C?

Not only is it OK to free() a void * value, by definition, all free() ever sees is a void * , so technically, everything freed in C is void * :-) @Daniel - If you ask me, it should be struct foo *p = malloc(sizeof *p)); but what do I know?


1 Answers

(int[2]) { 0, PAGE_SIZE }

is an expression (called compound literal) whose value is an array of two ints. The address of this array is taken, casted to void **, and dereferenced.

The net result is a reinterpretation of the array contents as a pointer to void.

Note that you can take the address of a compound literal, as they are lvalues. See eg. this question.

like image 149
Alexandre C. Avatar answered Sep 23 '22 22:09

Alexandre C.