getting the type of element from array C -
i wondering if possible in c type element in array. have following code, have manually specify type, remove second argument.
#define foreach( func, type, list)do {\ int num_elements = sizeof(list) / sizeof(type);\ int iter;\ for( iter = 0; iter < num_elements; iter++)\ func(list[iter]);\ }while(0) void display(int n) { printf("\n%d\n", n); } int main(void){ int list [] = { 1,2,3,4,5,6,7,8,9,10}; foreach(display, int, list); return 0; }
for purpose of question - can use sizeof
operator on elements of array -
sizeof(list[0])
if using gcc (or gnu), there extenstion called typeof
. can
sizeof(typeof(list[0]))
but c way of doing directly use sizeof
element.
Comments
Post a Comment