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 6582337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:16:02+00:00 2026-05-25T16:16:02+00:00

thanx k-ballo, u solved my prevoius problem but then i landed into another one!!

  • 0

thanx k-ballo, u solved my prevoius problem but then i landed into another one!!
i created nodes and then tried to display them, but every time i called append_node(), the double pointer **head_ptr(used to hold the address of the very first pointer *head, which in turn, holds the address of the very first node of the linked list), was holding a NULL value as if previous calls to append_node(&head, value) didnt add any node to *head.
so whenever i display the list, it reamins empty!! :

#include <stdio.h>

struct __node
{   int data;
    struct __node *next;
};

typedef struct __node node;

int append_node(node **head_ptr, int value)  //double pointer head_ptr to simulate call-by-reference
{   node *temp, *q;

    temp = (node *) malloc(sizeof(node));
    if(!temp)
    {   printf("\ninsufficient memory!!");
        return -1;
    }

    q = *head_ptr; //as *head_ptr is address of a pointer (which is *head), so any changes made after this line in q should also be reflected in main().. (i guess so!)

    temp->data = value;
    temp->next = NULL;

    if(q == NULL)
    {    q = temp;
         printf("\nq is empty");
         return 0;
    }

    while( q->next != NULL)
    {   q = q->next;
    }

    printf("\nq is not empty");
    q->next = temp;

    return 0;
}

int disp_list(node **head_ptr)
{   node *q;
    int i=1;

    q = *head_ptr;
    if(q != NULL)
    {   while( q != NULL )
        {   printf("|%d-%d|--->", i++, q->data);
            q = q->next;
        }
    }
    else
    {   printf("\nlist is empty!!");
    }

    return 0;
}

int main()
{   node *head=NULL;
    int value, res, i=0;

    while(i<3)
    {   printf("\nenter the data to be inserted into the node: ");
        scanf("%d", &value);
        res = append_node( &head, value);
            i++;    
    }

    printf("\nprinting all the nodes...\n") ;
    res = disp_list(&head);
    printf("\n---------------\nexiting...\n\n\n");  
    return 0;
}

i know i could have returned *q from append_node() and reassigned it to *head or declared *head as global.. but i want to *head to be manipulated by so-called-pass-by-reference method only. (theres no pass-by-reference actually in c!) my compiler is: gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

plz help..i m not an expert so please use easier terminology!! :p

  • 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-25T16:16:03+00:00Added an answer on May 25, 2026 at 4:16 pm

    Your initial call to append_node is passing a pointer to a pointer to node which is uninitialized (let’s pressume its null, though it will probably be just garbage). Then you do

    q = *head_ptr;
    //above statement causes a segment fault error..
    // that statement should be fine, we will get the value of main's head, which we pressume to be null
    
    // now we will try to dereference null by accesing its next element
    while( q->next != NULL)
    {   q = q->next;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've tried looking on google but i guess I can just not get the
I have tried to restrict Following EventHandler In specific Columns of Datagridview but fail
I'm doing an opengl/qt3 assignment, but I'm running into an undefined reference error: Renderer.h:
Safari gives resize handles for text-areas. anyone knows how to stop that? thanx
Thank you one and all ahead of time. I am currently in the process
thank you for looking i got this example from my book but i cant
I have a game that one player X wants to pass a ball to
im trying to create ball animation using gdi but i can't get it working.
iphone http://sharingdream.com/phone.jpg Dear all, I'm having a strange problem with an application I'm building
The problem is that this ball after it is dragged and exited click, it

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.