0

Code:

#include<stdio.h>
#include<malloc.h>
struct details{
  char *name;
  int no;
  struct details *info;
};

//ADDING THE LINKED LIST
void add(struct details **info,int no,char * name){
  struct details *temp=malloc(sizeof(struct details));
  temp = *info;
  if(temp == NULL){
      temp = malloc(sizeof(struct details));
  }
  else{
    if(temp->info == NULL){
       temp->info = malloc(sizeof(struct details));
       temp = temp->info;
    }
  }
  temp->no = no;
  temp->name = name;
}

//DISPLAYING THE LINKED LIST
void display(struct details *info){
  while(info!=NULL){
    printf("\nThe List is:\n","\n no: \tname:\n","%d","%s and link:%d",info->no,info->name,info->info);
    info = info->info;
  }
}

//MAIN PROGRAM
int main()
{
 struct details* ptr;
 char *name,ch;
 int no;
 int select_option;
 ptr = NULL;
 printf("\n   ***MAIN MENU***   \n1.Add Element \n2.Delete Element \n3.Search Element \n4.Linked List Concatenation \n5.Invert Linked List \n6.Diplay Elements \n Please Enter your choice:(eg:1,2,3,4,5,6)\n");
 scanf("%d",&select_option);
do{
 switch(select_option){
   case 1:
        printf("Enter no to add:");
        scanf("%d",&no);
        printf("Enter name to add:");
        scanf("%s",name);
        add(&ptr,no,name);
        break;
   case 6:
        display(ptr);
        break;
   default:
        printf("INVALID CHOICE!");
        break;
 }
 printf("Do u wish to continue?(y/n):");
 scanf("%c",&ch);
}while(ch == 'y' || ch == 'y');
 return 0;
}

I'm trying to write a simple program using a linked list to add and display data. But its throwing me a segmentation fault. I hope that I have initialized all the pointers with the memory. All help appreciated.

3
  • (gdb) run 10 Starting program: /home/beata/linked_List/p1/linked_list 10 MAIN MENU 1.Add Element 2.Delete Element 3.Search Element 4.Linked List Concatenation 5.Invert Linked List 6.Diplay Elements Please Enter your choice:(eg:1,2,3,4,5,6) 1 Enter no to add:12 Enter name to add:beata Program received signal SIGSEGV, Segmentation fault. 0x00178d98 in _IO_vfscanf () from /lib/libc.so.6 when debugged i could nnot get the proper information Commented Mar 14, 2012 at 12:57
  • That is NOT how you use a debugger. Find someone who can give you a demonstration of stepping through the code and examining variables. Commented Mar 14, 2012 at 13:06
  • thanks will ask someone to help me with the debugger Commented Mar 14, 2012 at 13:12

4 Answers 4

3

This:

temp->name = name;

does not copy name to temp->name but assigns temp->name to the same address as name, which is local to the function. You need to malloc() and strcpy():

temp->name = malloc(strlen(name) + 1);
strcpy(temp->name, name);

Remember to free(temp->name); when no longer required.

Additionally (as pointed out by Luchian), when reading from stdin:

char *name;
...
scanf("%s",name);

name has no memory allocated to. Declare it as an array but you need to protect against writing beyond the end of it:

char name[128];
...
scanf("%127s",name);
Sign up to request clarification or add additional context in comments.

2 Comments

You could always use strdup and save yourself the malloc call.
@RichardJ.RossIII, yep but AFAIK, strdup is not part of the C standard.
2

You haven't:

char *name;
///....
scanf("%s",name);

You never allocated memory for name.

You could do char name[50]; or whatever, but beware of overflows. Guard against them.

1 Comment

Thakns for remembering that. will be aware whenever i am handling arrays
1

Both hmjd and Luchian Grigore answers are a prior problems, but also:

You don't initialize temp->info at any point after a malloc, so it is never NULL, but yet temp->info is an invalid address.

Either initialize it explicitly or use calloc, which initializes the allocated buffer, instead of malloc.

Comments

1

You forgot to allocate name

     char *name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.