问题
I'm trying to copy one string to another in c using memcpy with the following code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
and it gives Segmentation fault (core dumped) Error after running the program
I tried to declare firstName and lastName as pointer to an array of chars rather than pointer to char but the error persists . what am I missing ?! plz help...
FYI .. I'm working on Ubuntu 14 and I'm using gcc (Ubuntu 4.8.2-19ubuntu1)...
回答1:
You are assigning myName.firstName and hisName.firstName with pointers to string literals. String literals cannot be modified, which is what's causing your error.
To achieve what you want, you can either declare firstName as a char array, or allocate memory to it (as a char pointer).
Array method:
typedef struct fullName
{
char firstName[256]; // a sufficiently large number
char lastName[256];
} NAME;
int main()
{
NAME myName, hisName;
strcpy(myName.firstName, "aaaaaaa"); // You can't assign a string directly
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
Allocation method:
typedef struct fullName
{
char* firstName;
char* lastName;
} NAME;
int main()
{
NAME myName, hisName;
size_t buffersize = 256; // a sufficiently large number
myName.firstName = malloc(buffersize);
hisName.firstName = malloc(buffersize); // same for lastName
strcpy(myName.firstName, "aaaaaaa");
strcpy(hisName.firstName, "bbbbbb");
memcpy(myName.firstName, hisName.firstName, buffersize); // You can't use sizeof() as this will give the size of a pointer
printf("myName.firstName = %s\n", myName.firstName);
printf("hisName.firstName = %s\n", hisName.firstName);
}
回答2:
In these statements
myName.firstName = "aaaaaaa";
hisName.firstName = "bbbbbb";
you initialized pointers with addresses of string literals.
In next statement
memcpy(myName.firstName, hisName.firstName, sizeof(hisName.firstName));
you try to modify one of the string literals.
According to the C Standard (6.4.5 String literals)
7 It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
Also this expression
sizeof(hisName.firstName)
returns size of the pointer itself. It is not the same as the size of the corresponding string literal.
The valid program could look the following way
#include <stdio.h>
#include <string.h>
typedef struct fullName
{
char firstName[8];
char lastName[8];
} NAME;
int main()
{
NAME myName = { "aaaaaaa" };
NAME hisName = { "bbbbbb" };
memcpy( myName.firstName, hisName.firstName, sizeof( hisName.firstName ) );
printf( "myName.firstName = %s\n", myName.firstName );
printf( "hisName.firstName = %s\n", hisName.firstName );
}
回答3:
This did not crash in MSVC, it produced the output:
myName.firstName = bbbbaaa
hisName.firstName = bbbbbb
Notice that only 4 chars from hisName have been copied to myName. That is because
sizeof(hisName.firstName)
is 4 on your platform, the size of the pointer. But manipulating such strings is Undefined Behaviour anyway.
来源:https://stackoverflow.com/questions/29523680/memcpy-error-segmentation-fault-core-dumped