This should work in practice, though I believe it relies on undefined behaviour and will generate warnings:
unsigned f(unsigned a, unsigned c) {
return &(((char*)a)[c]);
}
(fewer brackets are necessary in practice)
This works as follows:
(char*)a - cast 'a' to a 'char *' pointer
((char*)a)[c] - treat 'a' as an array, and index the c'th element, which
will be at address c + a
&(((char*)a)[c]) - take the address of that element, i.e. c + a
Finally the return
casts this back to unsigned
.
Trivial test harness which compiles with gcc 4.8 with two warnings:
#include <stdio.h>
unsigned
f (unsigned a, unsigned c)
{
return &(((char *) a)[c]);
}
int
main (int argc, char **argv)
{
unsigned a = 1;
unsigned b = 2;
unsigned c = f (a, b);
printf ("%d + %d = %d\n", a, b, c);
return 0;
}
Note as this is C, not C++, it may not compile with a C++ compiler.