Jump to content
  • Advertisement
Sign in to follow this  
thyrgle

Why is this linked list not working?

This topic is 2660 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to build a simple to chain linked with the following code:

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

int main (int argc, char * const argv[]) {
struct node *start_ptr = NULL;
struct node *newpointer = NULL;
newpointer->next = NULL;
start_ptr->next = newpointer;
return 0;
}


But, when I run I get EXC_BAD_ACCESS at newpointer->next = NULL; and do not know why. Any help on the matter would be appreciated.

Share this post


Link to post
Share on other sites
Advertisement
newpointer is NULL.. so you're trying to dereference a NULL pointer. You need to allocate memory for the node, for example with new or malloc. Is this supposed to be C or C++?

Share this post


Link to post
Share on other sites
Ok, well I actually got it to work by changing it to the following:

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

int main (int argc, char * const argv[]) {
node *start_ptr = new node;
node *newpointer = new node;
newpointer->next = NULL;
start_ptr->next = newpointer;
return 0;
}


But, I am confused on why the new node is need and and why I can't just use NULL?

Share this post


Link to post
Share on other sites

newpointer is NULL.. so you're trying to dereference a NULL pointer. You need to allocate memory for the node, for example with new or malloc. Is this supposed to be C or C++?


It is C++.

Share this post


Link to post
Share on other sites

Ok, well I actually got it to work by changing it to the following:

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

int main (int argc, char * const argv[]) {
node *start_ptr = new node;
node *newpointer = new node;
newpointer->next = NULL;
start_ptr->next = newpointer;
return 0;
}


But, I am confused on why the new node is need and and why I can't just use NULL?

If you don't allocate memory for your pointer to point to, then there is no next-member anywhere that you can access. NULL is not a pointer to a valid node object, which is why it cannot be used.

Share this post


Link to post
Share on other sites

But, I am confused on why the new node is need and and why I can't just use NULL?


newpointer is a POINTER to a node object
To access the members of the node object that newpointer is pointing to you use ->

If newpointer is set to NULL then it is not pointing to any valid node object. How can you access the members of an object when it isn't pointing to one?

Share this post


Link to post
Share on other sites
Well, how can NULL have a next field?
NULL indicates the end of a linked list. There is no life after NULL.

Share this post


Link to post
Share on other sites
Ok, I get it now, making a struct node* does not make an actual node so I have to make one that goes along with it. Thanks for everyone's help!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!