In some legacy code I have to maintain, & operators are put in front of arrays names whenever the arrays are to be passed as (void *) arguments
Here is a simple example :
char val = 42;
char tab[10];
memcpy(&tab, &val, 1);
It compiles with gcc or clang without errors or warnings. It also gives the expected result.
Is this syntax legal ?
Why does this works ?
Notes : I usually use one of the following syntax :
memcpy(tab, &val, 1);
memcpy(&tab[0], &val, 1);
Epilog :
As an additional test, I used a function taking a (char*) argument instead of (void*)
I get the following warning if I try to compile with clang :
warning: incompatible pointer types passing 'char (*)[10]' to parameter of type 'char *' [-Wincompatible-pointer-types]
Edit 1 : In the original example tab was given with a size of 1 element
I just changed the size to 10 for the sake of generality.
Edit 2 : As mentionned in the answers, memcpy takes (void*) and not (char*)
memcpy(a+0, &val, 1)just to cover third base too. (and I usually use your first example in your usual usage pattern).memcpy(&tab, &val, 1)?taband&tabare of different types. Both are valid only becausememcpy()takes arguments of typevoid*.memcpytakes arguments of typevoid*; see my previous comment and my answer.