I have a Segmentation fault (core dumped) when using strcpy, malloc, and struct
Posted
by
malsh002
on Stack Overflow
See other posts from Stack Overflow
or by malsh002
Published on 2013-10-29T03:40:33Z
Indexed on
2013/10/29
3:53 UTC
Read the original article
Hit count: 72
Filed under:
c
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.
© Stack Overflow or respective owner