Hi I am writing a simple C program using "struct". I am trying to add strings (which always seems to be trouble) and I finally get to compile but when I run it I get an error. The file name is "struct" this is the error:
*** stack smashing detected *** ./struct terminated
Aborted (core dumped)
Here is the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct members {
int id;
int age;
char name[];
};
int main(void)
{
struct members person;
person.id = 1223;
person.age = 37;
strcpy(person.name, "Matthew");
printf("ID: %d\n Age: %d\n Name: %s\n", person.id, person.age, person.name);
return 0;
}
I have no idea why it's compiling fine then crashing. I guess it's a memory problem with the code in relation to the string. Funny thing is that it actually works and prints the info to console and then it crashes. Any help is appreciated. Thanks.