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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:37:09+00:00 2026-05-22T14:37:09+00:00

Sorry for the silly title. For part of an (very basic) assignment we’re implementing

  • 0

Sorry for the silly title.

For part of an (very basic) assignment we’re implementing a stack with pointers. I’m having a lot of trouble with one small part, so I’ve isolated it into this small problem.

I will try to explain my problem, but reading the code will probably be easier to understand.

There is a structure (named node) which has 2 members, a char (named data), and a pointer to another node (named next).

Inside the main function I have a pointer named head which points to node1, I want to pass this pointer to another function, and make it point to a new node (and make this new node point to yet another new node). I think I might be okay with setting the pointer to a new node, but I can’t correctly get that new node to point to another new node correctly.

#include <stdio.h>

struct node {
    char data;
    struct node *next;
};

void modifyPtr(struct node **p);

int main(void)
{
    /* create first 2 nodes */
    struct node n1;
    n1.data = '1';

    struct node n2;
    n2.data = '2';

    /* set 1st node's next node to the 2nd node */
    n1.next = &n2;

    /* create a pointer to a node and make it point to the first node */
    struct node *head = &n1;

    /* this works as expected */
    printf("1. %c\n", head->data);
    printf("2. %c\n", head->next->data);

    /* this should set head to a new node (which in turn points to another new node) */
    modifyPtr(&head);

    /* this prints as expected. Am I just lucky here? */
    printf("3. %c\n", head->data);
    /* but this doesn't. I want it to print 4. */
    printf("4. %c\n", head->next->data);
}

void modifyPtr(struct node **p)
{
    /* create node 3 and 4 */
    struct node n3;
    n3.data = '3';

    struct node n4;
    n4.data = '4';

    /* set node3's next node to node4 */
    n3.next = &n4;

    /* make p point to node 3 */
    *p = &n3;
}

I expect to see the output as

  1. 1
  2. 2
  3. 3
  4. 4

but instead I get

  1. 1
  2. 2
  3. 3
  4. |

I’ve been trying to get this to work for ages. I’m thinking that maybe it’s to do with creating the nodes in the local scope of modifyPtr and trying to use them in main. But then I don’t see why #3 would work.

Can someone tell me what I’m doing wrong? Thanks.

  • 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-22T14:37:10+00:00Added an answer on May 22, 2026 at 2:37 pm
    void modifyPtr(struct node **p)
    {
        struct node n3;
        n3.data = '3';
        ...
        *p = &n3;
    }
    

    n3 and n4 are local variables*, so they cease to exists once modifyPtr returns. You need to allocate them on the heap.

    void modifyPtr(struct node **p)
    {
        struct node *pn3 = malloc(sizeof(struct node));
        pn3->data = '3';
        ...
        *p = pn3;
    }
    

    You just got lucky that n3.data didn’t get clobbered.

    * — Laymen speak.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is going to sound too silly / too basic - sorry about that,
I know that the title might seem silly, couldn't think of something better, sorry.
Sorry, probably another silly question, but I've got a lot of information to put
Sorry for this silly question, but is there any way to restrict using directives
Sorry for such a trivial question, but I'd feel silly building this if it
I am new to Microsoft.MVC, so sorry if this is a silly question. I
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for the silly question, but I ran across code that used: <?=$MAP_OBJECT->printOnLoad();?> <?=$MAP_OBJECT->printMap();?>
sorry for the silly question, but i am stuck converting for example the following

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.