问题
This has been bugging me for a while.
struct person {
char name[15];
int age;
};
struct person me;
me.name = "nikol";
when I compile I get this error:
error: incompatible types when assigning to type ‘char[15]’ from type ‘char *’
am I missing something obvious here?
回答1:
Arrays are second-class citizens in C, they do not support assignment.
char x[] = "This is initialization, not assignment, thus ok.";
This does not work:
x = "Compilation-error here, tried to assign to an array.";
Use library-functions or manually copy every element for itself:
#include <string.h>
strcpy(x, "The library-solution to string-assignment.");
回答2:
me.name = "nikol";
is wrong !! you need to use strcpy()
when you do x = "Some String"
, actually you are putting the starting address of the static string "Some String"
into variable x
. In your case, name
is a static array, and you cannot change the address. What you need, is to copy your string to the already allocated array name
. For that, use strcpy()
.
回答3:
First of all, you need to know the following points:
- In C, text strings are just arrays.
- In C, array variables are basically just pointers.
So, char mytext[12];
is essentially just declaring a char pointer called mytext
that stores the address of the first (zero'th) element of the array/string.
This code is therefore valid:
#include <stdio.h>
int main(int argc, char *argv[])
{
const char a[] = "Hello";
const char *b = a;
printf("%s\n", b);
return 0;
}
The important thing to note here is that re-assigning b
doesn't change the contents of whatever it points to - it changes the thing that it points to.
However, there are cases where arrays and pointers behave differently. In the example above, a
cannot be reassigned. If you try, you'll get an error.
To go back to you original example, this structure:
struct person{
char name[15];
int age;
};
...can be thought of as a 19-byte structure* of which the first 15 bytes are earmarked for storing a string. The name
attribute stores the address of the first byte, so you know where those 15 bytes live in memory - you just need to write something useful into them.
This is where functions such as sprintf()
or strcpy()
come into play - they copy data into the address defined by name
rather than re-assigning name
itself.
* Assuming that sizeof(int)
is 4 and the structure is not padded, of course...
回答4:
The initialization:
char name[15]
is creating a char array, 15 bytes in size.
"nikol" is a string literal, which is type 'char *', i.e. a pointer to a string. They are incompatible types. You need to copy the string into the array, like so:
strcpy(me.name, "nikol")
回答5:
Use strcpy() function (of string.h
library) :)
main(){
struct person{
char name[15];
int age;
};
struct person me;
strcpy(me.name,"nikol");
}
回答6:
You are trying to change the memory address of an array pointer (by giving me.name = "nikol") which is impossible. Name of array (me.name) is a fixed pointer and cannot be changed to point to a new location of string ("nikol") because memory address of an array pointer is on the first member of that array. If you want to assign a string after declared (me.name = "nikol"), use external pointer *name instead because external pointer floats outside array so its value can be changed on-the-fly.
struct person {
char *name;
int age;
};
struct person me;
me.name = "nikol"; // no error
来源:https://stackoverflow.com/questions/26693537/in-c-why-cant-i-assign-a-string-to-a-char-array-after-its-declared