Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7649719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T11:02:33+00:00 2026-05-31T11:02:33+00:00

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

  • 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T11:02:34+00:00Added an answer on May 31, 2026 at 11:02 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

consider the code #include<stdio.h> int main(void) { char* a; scanf(%s,a);//&a and &a[0] give same
When I run this code: #include <stdio.h> typedef struct _Food { char name [128];
CODE: #include <stdio.h> main() { int nums[100], i; char answer; int count = 0;
Here is my code: #include <stdio.h> int main(void) { FILE *fp; unsigned int i;
I have the following code: /* sample.c */ #include<stdio.h> #include<malloc.h> #include<stdlib.h> #includehermes.h #include<string.h> int
see my code is #include <stdio.h> #include<stdlib.h> #include<sys/stat.h> int main(int argc, char**argv) { unsigned
This is code for a linked list in the C programming language. #include <stdio.h>
I have the following C-code: #include<stdio.h> #include<stdlib.h> typedef struct node { int a; }node;
Code #include<stdio.h> int main() { int i; printf(%d \n,'\1'); printf(%d \n,'\022'); printf(%d ,'\555'); return
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.