I can't seem to figure out why strcpy
makes a segmentation fault in this code. It should be straightforward:
typedef struct message {
char *buffer;
int length;
} message_t;
int main () {
char* buf = "The use of COBOL cripples the mind; its "
"teaching should, therefore, be regarded as a criminal "
"offense. -- Edsgar Dijkstra";
message_t messageA = {"", 130};
message_t *message = &messageA;
strcpy(message->buffer, "buf");
printf("Hello\n");
}
EDIT: strcpy(message->buffer, "buf")
is supposed to be strcpy(message->buffer, buf)
without the "" quotes
EDIT: Thank you to the comments! This has been resolved by malloc'ing message->buffer to make space for buf:
message_t messageA = {"", 130};
message_t *message = &messageA;
message->buffer = malloc(122);
strcpy(message->buffer, buf);
printf("Hello\n");