Problem with pointers

This is a discussion on Problem with pointers within the C Programming forums, part of the General Programming Boards category; I'm having some problems doing the insert function for a linked list. I have the basic struct Code: typedef struct ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    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

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,627
    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;
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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
    Last edited by QuantumPete; 06-12-2008 at 04:54 AM. Reason: lack of proper touch typing skills
    "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

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    101

    Thank YOU!

    Oh my God!

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

    Thank you so much!

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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