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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:15:27+00:00 2026-06-03T21:15:27+00:00

I am new to C, and am trying to implement a stack with linked

  • 0

I am new to C, and am trying to implement a stack with linked list. Currently set up the Stack, all good so far. The problem has come in when I try to push a new node onto the list.
I currently have

In main(), push() is called by:

push(&(s.head), 'r');

The function push is:

void push(StackNodePtr *topPtr, char value){
    printf("topPtr value %c", (*topPtr)->data); // - Is currently 'p'

    StackNodePtr sNP;
    sNP = malloc(Node_Size);
    sNP->data = value;                          // - Is currently 'r'
    sNP->nextPtr = *topPtr;

    printf("\nsNP value - %c", sNP->nextPtr->data);      // Prints p... cool
    topPtr = &sNP;      // Just assigned it???
    printf("\ntopPtr at end of push = %c", (*topPtr)->data);    // prints r... cool
    // WHY YOU NO REFERENCE sNP LATER!?!?
}

Meanwhile, back in main:

    printf("\non the stack...%c", stackTop(s.head));  // prints 'p'

It seems to work fine in push, however I call printf() on the node that topPtr pointed to and the value that topPtr used to be prints out instead (in this case ‘p’). As far as I can tell from the hunting I’ve done, it looks and feels correct and I don’t know what I have missed.

Could it be where I have done topPtr = &sNP;?

Any “push” in the right direction is a good push…

  • 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-06-03T21:15:28+00:00Added an answer on June 3, 2026 at 9:15 pm
    topPtr = &sNP;      // Just assigned it???
    

    No, you didn’t. You assigned the value to local copy of pointer. You change the value of topPtr itself, and it is does not go outside. Instead, you should write to location it points to:

    *topPtr = sNP;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All I am trying to do is XmlSerializer serializer = new XmlSerializer(typeof(Stack<int>)); and I
I'm trying to implement OpenId login for a web application. Whenever new user who
I am new to python and I am trying to implement rfind function using
I'm quite new to C and I'm trying to implement a binary tree in
I am new to Objective C and I am trying to implement an async
I'm fairly new to iPhone programming and am trying to implement a double-component PickerView.
I am new to both MVC and Stackoverflow I am trying to implement pagination
I'm new to AS3 and I'm getting this error while trying to implement OO
I'm new to python but I've run into a hitch when trying to implement
I am new to OCaml, and I am now trying to implement a function

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.