c preprocessor - Pointer to type within a macro, C -


i've run problem when attempting express pointer type within macro.

take simple example.

#define index_of_data(data, type, index) \     ((type *)data)[index] 

this works:

index_of_data(buffer, float, 3); 

where fails:

index_of_data(buffer, float[2], 3); 

because cast should (float(*)[2]).

is there way express "the pointer of type", ...without using typeof? (which isn't standard c).


note, there of course other ways make specific example work. cast char , offset sizeof(type) * index example. interested in way express pointer type in c.

doing heavily complicated stuff c types may fun sport, it's extremely confusing in real code.

to avoid wrecking heads on complicated pointer expressions much, people use typedef!

if typedef types expect use macro of yours, won't have problems. observe:

#include <stdlib.h>  #define index_of_data(data, type, index) \     ((type *)data)[index]   int main(void) {     float (*buffer)[2] = malloc(4 * sizeof(float[2]));      typedef float t_f2arr[2];      index_of_data(buffer, t_f2arr, 3)[0] = 1.1f;      return 0; } 

this intended!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -