Problem with pointers

• 06-12-2008
kotoko
Problem with pointers
I'm having some problems doing the insert function for a linked list.

I have the basic struct
Code:

```typedef struct Node {         Data* data;         struct Node* next;         } No;```
And the method to insert:
Code:

```void insert (Node* node) {         Node* tmp, tmpProx;                 if (head == NULL)         {                 head = node;         }         else         {                 for(tmp = head; tmp->next != NULL  ||  calcDist(tmp->next->restaurante) > calcDist(no->restaurante)  ; tmp = tmp->next );                                 // Problem Here!                 tmpProx = tmp->next;                 tmp->next = node;                 node->next = tmpProx;                 // End of Problem                         } }```
I'm having the error
Code:

`error: incompatible types in assignment`
Can someone explain me what the problem is please? I'm totally lost :(
• 06-12-2008
Elysia
Code:

`Node* tmp, tmpProx;`
An evil pitfall in the language.
tmp is of type Node*, but tmpProx is of type Node.

To solve it, do either:
Code:

`Node *tmp, *tmpProx;`
...or my preferred way...
Code:

```Node* tmp; Node* tmpProx;```
• 06-12-2008
QuantumPete
That's because you've committed one of the cardinal sins of declaring pointers!
When you write:
Code:

`int * a, b;`
You're declaring a to be a pointer to an int, and b to be an int! It is equivalent to:
Code:

```int * a; int b;```
You end up trying to assign tmp->next, which is a Node* to tmpProx, which is just a Node.
My recommendation is always to declare and initialise variables on their own line, i.e.
Code:

```Node * tmp = NULL; Node * tmpProx = NULL;```
QuantumPete
Edit: Damn you Elysia ;)
• 06-12-2008
kotoko
Thank YOU!
Oh my God!

I would never remember to check that in a million years!

Thank you so much!