Why this code portion give compilation error?

int main()
    char c[10];
    char *cp="Test";


this give compile time error
|warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]

but changing char c[10] into 2D array issue solved ,Why??


you’re passing the value of the first element of the c array to strcpy which expects a pointer

strcpy (c,cp);

Did you forget something? strcpy((char*)&c[0],cp);

With an array, the name of the array, c, is a pointer to the array, so no cast is necessary.

When you cast c[0] to char *, you are telling the compiler to take the value stored in c[0] and use that as a char *. The compiler is complaining because a pointer is stored as a two-byte value (on most arduino boards), but c[0] is a single byte. Changing to a two dimensional array, such as c[5][2], makes c[0] refer to the two char stored in c[0][0] and c[0][1], so the compiler is now satisfied that you are giving it two bytes of memory. There will be no warning/error message, but strcpy will not write to the array, but to whatever memory address is pointed to by the value stored in c[0].

thanks alot



Is also wrong. The first argument to printf MUST be a pointer to a char array, NOT a single char.

Regards, Ray L.