0
struct Person{
  char *name;
  int numb;
  char *var;
};

struct Node{
   struct Person *data;
   struct Node *next;
   struct Node *prev;
};


int main() {

head=(struct Node*)(malloc(sizeof(struct Node)));//
tail=(struct Node*)(malloc(sizeof(struct Node)));//These two are global variables and they are initialized with null above.

 struct Person *a1=(struct Person*)(malloc(sizeof(struct Person)));
 char is[]="nameee";
 strcpy(a1->name,is);

 printf("%s\n",a1->name);



  return 0;
}

why i am getting segmentation fault for that code? I created a doubly linked list stored Person but when i initialized the structure with a string this code is getting an error.

Leo S
  • 319
  • 2
  • 16
  • 2
    Do not cast the return value of malloc. – Tordek Nov 20 '19 at 10:12
  • `a1->name` is an uninitialised value. You need to allocate dynamic memory or declare that as a fixed size char array. – kaylum Nov 20 '19 at 10:15
  • You have not allocated any space for `a1->name` – klutt Nov 20 '19 at 10:15
  • It did not work. I removed (struct Person*) but still getting an error. I am new in c so can you explain a little more.@Tordek – Leo S Nov 20 '19 at 10:15
  • My comment was not meant as a solution but as a good practice. By casting the return value of `malloc` you're hiding potential bugs, and obscuring code unnecessarily. – Tordek Nov 20 '19 at 10:18
  • Please provide a [mvce], a complete compilable program. Casting the return value of malloc and *not* having includes can **exactly** cause crashes too. If you do not provide the code I am going to assume that's the reason. – Antti Haapala -- Слава Україні Nov 20 '19 at 10:26

4 Answers4

0

That's because you're not allocating memory for a1->name. Sure, you're allocating memory for a1, which includes the pointer a1->name, but name is not pointing anywhere valid. You need to allocate memory for that, too:

a1->name = malloc(sizeof("nameee"));

And don't forget to free all your memory when you're done:

free(a1->name);
free(a1);
free(head);
free(tail);

These two are global variables and they are initialized with null above.

To have a Minimal, Reproducible Example is i helpful if you include parts like that in your code, like this:

#include<stdio.h>
#include<stdlib.h>

struct Node {
    struct Person *data;
    struct Node *next;
    struct Node *prev;
} *head, *tail;
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • Thanks for your helpful comment. This is not related under the topic but i wonder that if i initialize header with malloc. The types of the node class are initialized NULL? And also i have created the head with struct Node* head=NULL so what if i do struct Node head=NULL these two are different? I am new on c and these type of questions are confusing.@Blaze – Leo S Nov 20 '19 at 10:34
  • @XY `head` is just a pointer. When you assign to it with `head=malloc(sizeof(struct Node));` it overwrites the value it previously had. With `struct Node head=NULL` you're making a pointer and setting its value to `NULL`, but it's not allocating any memory for it yet. – Blaze Nov 20 '19 at 10:39
0

a1->name is not initialized, you should malloc it or change it to an array like

#define MAX_NAME_LENGTH 30

struct Person{
  char name[MAX_NAME_LENGTH];
  int numb;
  char *var;
};```
Damiano
  • 698
  • 7
  • 19
0
struct Person *a1 = malloc(sizeof(struct Person));
char is[] = "nameee";
strcpy(a1->name,is);

strcpy copies a string into a memory location (in this case, a1->name); it needs to be allocated. a1->name's value is uninitialized, thus you are writing in an undefined location in memory.

You need to allocate space for it, for example like:

struct Person *a1 = malloc(sizeof(struct Person));
char is[] = "nameee";
a1->name = malloc(strlen(is) + 1);
strcpy(a1->name,is);

or, use strdup.

Tordek
  • 10,628
  • 3
  • 36
  • 67
0

You are allocating a variable of type struct Person. The field nameis just a pointer. But there is no space allocated for the string that name points to.

You need to allocate the string itself, something like: a1->name = malloc(xxx);

Guillaume Petitjean
  • 2,408
  • 1
  • 21
  • 47