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

  • Home
  • SEARCH
  • 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 510667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:08:14+00:00 2026-05-13T07:08:14+00:00

I wanted to test the following code (which works fine for a non-null list)

  • 0

I wanted to test the following code (which works fine for a non-null list) to see what would happen in the case of an empty list (in which case the head would be null).

hence the code which applies to filling the list is commented out..

But for some strange reason, the test for NULL in print_nodes() just doesnt seem to work. ive added some debug cout calls to see (and also checked using gdb) but whilst the value does indeed appear to be NULL, any if statements dont seem to test the equivalence properly..

any idea why?

many thanks!

   #include <iostream>
using namespace std;

struct node {
  char dat;
  node *nextPtr;
};

//inserts new node and returns pointer
node* new_node(char data, node* prevNode);

//adds a new node at the head ofthe list
void new_head (node *head_, char dat_);

//inserts new node after *before
void insert_node (node *before, char dat_);

//runs through and prints the list - requires first node (head)
void print_nodes (node *head);


int main() {
  cout <<endl << endl;
  cout << endl << "*******************RUN******************" <<endl <<endl;

  node* head = NULL;

  if (head == NULL) { 
    cout << "head null"; //this works here
  }


  //head non-standard
  //  node* head = new node;
  //  head->dat ='a';

  /*
  node* b = new_node('b', head);
  node* c = new_node('c', b);
  node* d = new_node('d', c);
  node* e = new_node('e', d);
  node* f = new_node('f', e);

  */
  print_nodes(head);

  insert_node(head,'N');

  print_nodes(head);

  cout << endl << "*******************END RUN******************" <<endl;
  return 0;
}

node* new_node(char data, node* prevNode) {
  node* tempPtr = new node;
  tempPtr->dat = data;
  tempPtr->nextPtr = NULL; //standard
  prevNode->nextPtr = tempPtr;
  return tempPtr;
}

void new_head (node *head_, char dat_) {

}

void insert_node (node *before, char dat_) {
  node* tempPtr = new node;
  tempPtr->dat = dat_;

  tempPtr->nextPtr = before->nextPtr;
  before->nextPtr = tempPtr;

}


void print_nodes (node *head) {

  node* tempPtr = head;

  cout << "\nPrinting nodes..." <<endl;

  if (tempPtr == NULL) { //this test is not working.. why?
    cout << "tempPtr is NULL";
    return; 
  } else { //only run in the non null case
    for (tempPtr; tempPtr != NULL; tempPtr = tempPtr->nextPtr) {
      cout << "Current node content: " << tempPtr->dat <<endl;
    }  
  }
}
  • 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-13T07:08:14+00:00Added an answer on May 13, 2026 at 7:08 am

    You have a problem: head was not allocated, but insert accesses its “next element”:

    before->nextPtr = tempPtr;
    

    head is passed in as before, and you didn’t allocate memory for head. Hence you dereference a NULL pointer here.

    Could it be that your application crashes as a result, and the printout to cout isn’t done because cout is buffered?

    Try to:

    1. Remove the call to insert
    2. Change cout to cerr (unbuffered)

    Report the results of these changes.

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

Sidebar

Ask A Question

Stats

  • Questions 314k
  • Answers 314k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer all that needs to be done is for to define… May 13, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer in your css file add this.... a:hover { cursor:pointer; }… May 13, 2026 at 11:09 pm
  • Editorial Team
    Editorial Team added an answer Cast them to (byte). May 13, 2026 at 11:09 pm

Related Questions

I'm writing an iPhone app that is mainly centered around grid patterns, so I
I'm trying to trigger a button event in a gridview. I created a gridview
I just wanted to see if I could have your thoughts on the design
I came across something very basic but extremely bewildering today. I needed to convert
I'm using VB 2008 and I'm trying to add a xmlns=mynamespace attribute to an

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.