Linked list problem

I cant figure out whats wrong here?

I've got the following errors:
Error 1 error C2440: '=' : cannot convert from 'p_node *' to 'int'
Error 2 error C2227: left of '->fname' must point to class/struct/union/generic type

Error 2 is because of error 1.

But how do I get a temp-pointer to work? My code below is from a function that im calling within the program.

I got my help from this link:
http://richardbowles.tripod.com/cpp/linklist/linklist.htm
Read the topic "Adding a node to the end of the list", I can't figure out how to create that temp = new node;?



1
2
3
4
5
6
7
8
9
10
11
12
13
p_node * person_add(void)
{

    int* start_ptr = NULL;

    int *temp = NULL;

    *temp = new p_node;

    cout << "Enter first name: " << endl;
    cin >> temp -> fname;
        
}
You're declaring 'temp' as a pointer to an int, but then making it point to a p_node. This is nonsensical.

You're also dereferencing a null pointer on line 8 (bad).

If 'temp' points to a p_node, then you should make it a p_node pointer. 'new' also returns a pointer, so you assign it to the pointer. You don't dereference the pointer:

1
2
3
p_node* temp = NULL;  // note:  p_node*..  not int*

temp = new p_node;  // note:  temp = ... not *temp =  


Of course initializing to NULL can also be skipped:

 
p_node* temp = new p_node;
Topic archived. No new replies allowed.