C macro overloading for N-D arrays assignment -


there many questions/answers macro overloading. but, cannot find way apply particular problem.

i conveniently assign values 3d images in c. now, follow:

#define imget(im,y,x,c) im.data[(y)+im.height*((x)+im.width*(c))] #define imset(im,y,x,c,v) imget(im,y,x,c)=v 

it works well.

but, use when have black&white images, 2d. something, that:

#define imget(im,y,x,c) im.data[(y)+im.height*((x)+im.width*(c))] //3d case #define imset(im,y,x,c,v) imget(im,y,x,c)=v //3d case #define imget(im,y,x) im.data[(y)+im.height*(x)] //2d case #define imset(im,y,x,v) imget(im,y,x)=v //2d case 

is possible?

the comment of @potatoswatter gives , general solution. may overkill simple problem.

after looking @ it, have found following solution:

#define imget(im,y,x,...) im.data[(y)+im.height*((x)+im.width*(__va_args__+0))] 

then, can use macro following situations:

float val = imget(im,y,x,c); 

or:

float val = imget(im,y,x,c); 

and if want assingn, do:

imget(im,y,x,c)=val; 

or

imget(im,y,x)=val; 

Comments