Look at the strange line from function f:
typedef char thing[1];
void f(thing t){
thing *p;
/* Strange line. Changing t to &t is wrong.
C++ require a (thing *) cast.
*/
p = t;
*(*p) = 'C';
}
int main(int argc, char* argv[])
{
thing g;
thing *h;
/* Works as expected. */
h = &g;
g[0] = 'A';
*(*h) = 'B';
f(g);
/* g[0] is now 'C' */
return 0;
}
Changing f to
void f(thing t){
char **p;
p = &t; /* note the & */
*(*p) = 'C';
}
also works.
I was expecting p=&t to be the right instruction, not p=t.
After all t is a thing and &t must be a thing*.
What happened in that strange line?
Just an alternative solution:
You can prevent the array-to-pointer decay by passing the array by reference. This should work as you want: