I have a Segmentation fault (core dumped) when using strcpy, malloc, and struct
- by malsh002
Okay, when I run this code, I have a segmentation fault
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 64
struct example {
char *name;
};
int main()
{
struct example *s = malloc (MAX);
strcpy(s->name ,"Hello World!!");
return !printf("%s\n", s->name);
}
the terminal output:
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ make q1
cc -Wall -g q1.c -o q1
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ ./q1
Segmentation fault (core dumped)
alshamlan@alshamlan-VGN-CR520E:/tmp/interview$ gedit q1.c
Can someone explain what's going on? thanks.