C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2008, 04:10 AM   #1
Registered User
 
Join Date: Apr 2008
Posts: 75
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
kotoko is offline   Reply With Quote
Old 06-12-2008, 04:45 AM   #2
Mysterious C++ User
 
Elysia's Avatar
 
Join Date: Oct 2007
Posts: 14,785
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;
__________________
Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System
I dedicated my life to helping others. This is only a small sample of what they said:
"Thanks Elysia. You're a programming master! How the hell do you know every thing?"
Quoted... at least once.
Quote:
Originally Posted by cpjust
If C++ is 2 steps forward from C, then I'd say Java is 1 step forward and 2 steps back.
Elysia is offline   Reply With Quote
Old 06-12-2008, 04:53 AM   #3
Technical Lead
 
QuantumPete's Avatar
 
Join Date: Aug 2007
Location: London, UK
Posts: 723
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
__________________
"No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
"Have you tried turning it off and on again?" - The IT Crowd

Last edited by QuantumPete; 06-12-2008 at 04:54 AM. Reason: lack of proper touch typing skills
QuantumPete is offline   Reply With Quote
Old 06-12-2008, 05:17 AM   #4
Registered User
 
Join Date: Apr 2008
Posts: 75
Thank YOU!

Oh my God!

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

Thank you so much!
kotoko is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
hi , i'm having a problem with pointers in C ... blackslither C Programming 2 10-26-2008 03:57 AM
Problem with file handling (pointers) hmk C Programming 5 09-19-2008 10:03 AM
A problem with pointers vsla C Programming 2 10-10-2007 04:14 AM
Returning pointer to array of pointers problem jimzy C Programming 15 11-11-2006 06:38 AM
Problem writing swap using pointers joshdick C++ Programming 1 02-29-2004 10:06 PM


All times are GMT -6. The time now is 03:24 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22