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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:15:27+00:00 2026-05-15T05:15:27+00:00

Sorry if this question has been asked before. On my search through SO I

  • 0

Sorry if this question has been asked before. On my search through SO I didn’t find one that asked what I wanted to know.

Basically, when I have this:

typedef struct node
{
    int data;
    node *node;
} *head;

and do node *newItem = new node;

I am under the impression that I am declaring and reserving space, but not defining, a pointer to struct node, is that correct?

So when I do

newItem->data = 100 and newItem->next = 0

I get confused. newItem = 0would declare what exactly? Both data and next? The object as a whole?

I’m especially confused when I use typedef. Which part is the macro? I assume node because that’s how I call it, but why do I need it?

Finally, what happens when I do:

node *temp;
temp = new node;

temp = head->next;
head->next = newItem;
newItem->next = temp;

I mean, head->next is a pointer pointing to object newItem, so I assume not to newItem.data or next themselves. So how can I use an uninitialized pointer that I described above safely like here? is head now not pointing to an uninitialized pointer?

  • 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-15T05:15:27+00:00Added an answer on May 15, 2026 at 5:15 am

    I am under the impression that I am
    declaring and reserving space, but not
    defining, a pointer to struct node, is
    that correct?

    No. You are declaring a pointer, allocating space on the stack for the pointer, and dynamically allocating storage for a node to it it.

    Don’t confuse yourself by writing stuff like this:

    typedef struct node
    {
        int data;
        node * next;
    } *head;
    

    The way to write the struct in C++ is:

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

    You can now create a pointer:

    node * pnode;
    

    which allocates storage for the pointer.

    and you can dynamically allocate storage for a node, and make the pointer point to it:

    pnode =  new node;
    

    or do it all in one:

    node * pnode = new node;
    

    Now when you say:

    pnode->data = 10;
    

    you are not allocating anything. You are assigning 10 to the member called data of the node instance pointed to by pnode. Of course, if you had given your node a constructor (which you should normally do), you could do it all in one:

    struct node
    {
        int data;
        node * next;
    
        node( int n, node * np ) : data( n ), next( np ) {}
    };
    
    node * pnode = new node( 10, 0 );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am sorry if this question has been asked before (I tried to find
sorry if this question has been asked before, I searched but wasn't sure on
So, if this question has been asked before, I'm sorry. I'm not exactly sure
I'm sorry if this question has been asked before, but I'm not even sure
Sorry if this has been asked before, but I can't find a clear enough
I'm sorry if this has been asked before, but I looked through the related
First of all, sorry if this has been asked before. I've done a pretty
This question has been asked before but 1) the user never accepted an answer
I know this is a question that has been discussed over and over, but
OK, I know that this has been asked previously, so please forgive me for

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.