Why is the following ok?
char *a;
char b[]="asdf";
a=b;
But the following is not?
char a[10];
char b[]="asdf";
a=b;
The above gives error: incompatible types in assignment.
Why is the following ok?
char *a;
char b[]="asdf";
a=b;
But the following is not?
char a[10];
char b[]="asdf";
a=b;
The above gives error: incompatible types in assignment.
When you say
'a' is actually
and 'a' is initialized to point to 10 * sizeof(char) of allocated memory.
So
are allowed. But
is not allowed.
The value of an array evaluates to the address of the first element within the array. So basically, it's a constant value. That's why when you try to do a=b in the second example, you're trying to do something similar to 2=7, only you have two addresses instead of 2 integers.
Now it makes sense that the first example would work, since assigning an address to a pointer is a valid operation.
Please use strcpy_s function of c++, it is having a syntax of &dest,*source it might help.
Here you are assigning address of array
b
toa
which is ofchar
type. Size of address will be 4 bytes (8 bytes in 64 bit m/c) which you are assigning to 1 bytechar
variablea
so the values will get truncated. This is legal but its of no use.I think you are actually trying to assign first character of
b
array toa
. In that case doa = b[0]
.Both are not ok.
Maybe you were attempting ,