passing argument from incompatible pointer type

This is a discussion on passing argument from incompatible pointer type within the C Programming forums, part of the General Programming Boards category; Hi, Note: Sorry, I can't get the indenting to work properly! Let me start off by saying I'm an entirely ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    40

    passing argument from incompatible pointer type

    Hi,

    Note: Sorry, I can't get the indenting to work properly!

    Let me start off by saying I'm an entirely self taught programmer (although I'm not sure it's accurate or fair to describe myself as a programmer), so there may be some stylistic issues presented in the code below. If you have any comments / suggestions as to how to improve the follow bit of code in general, I'd appreciate it. However, I have a specific question regarding a compiler warning that I find confusing.

    The following code compiles and runs as I expect it to, yet I generate a number of 'passing argument from incompatible pointer type' errors, and I'd like to know what I'm doing wrong. I'm concerned that while it currently appears to be doing what I want it to do, that it's actually only working by chance (i.e. the code works, but for the wrong reasons), which would mean it would potentially break in the future.

    I'm compiling using gcc 4.03 on OS X 10.6.2, though I haven't tried to run this through Xcode directly.

    Here is the code:

    typedef struct
    {
    int data;
    int depth;
    struct node *parent_ptr;
    struct node *left_ptr;
    struct node *right_ptr;
    } node;



    node *addNode(node *n, node* nprev, int data, int depth, int maxdepth)
    {

    if (n==NULL && depth < maxdepth)
    {


    printf("null pointer found, creating node.\tdepth = %d\n", depth);
    n= calloc(1, sizeof(node));
    n->data= data;
    n->depth= depth;
    if (n->left_ptr == NULL)
    {

    n->left_ptr= addNode(n->left_ptr, n, n->data*2, n->depth+1, maxdepth);
    }

    if (n->right_ptr == NULL)
    {

    n->right_ptr= addNode(n->right_ptr, n, n->data*2+1, n->depth+1, maxdepth);
    }

    }

    return n;
    }

    Compiling returns the error passing argument 1 of ‘addNode’ from incompatible pointer type from the line

    n->left_ptr= addNode(n->left_ptr, n, n->data*2, n->depth+1, maxdepth);

    I'm not sure why. n->left_ptr, is a pointer, and the definition of addNode is node *addNode(node *n, node* nprev, int data, int depth, int maxdepth), so it should be expecting a pointer to a node, which is exactly what I'm passing it.

    Is it because I'm passing a pointer to a pointer, in which case the definition should be
    *addNode(node **n, node **nprev, int data, int depth, int maxdepth),

    and then when I call it with a single pointer, include a dereferencing &?

    Cheers,
    Brad
    Last edited by bhdavis1978; 03-17-2010 at 01:29 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do not have such a thing as a struct node anywhere in your code. You have an unnamed struct, which you typedef to "node".

    Your code should start with
    Code:
    typedef struct node
    {
    int data;
    int depth;
    struct node *parent_ptr;
    struct node *left_ptr;
    struct node *right_ptr;
    } node;

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Can you include the section in main or wherever you are calling that function?

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    40
    awesome!

    Thanks tabstop! What is the difference between what I had done, and what you showed? How does that differ from
    Code:
    typedef struct node
    {
    int data;
    int depth;
    struct node *parent_ptr;
    struct node *left_ptr;
    struct node *right_ptr;
    };

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    check this thread out
    typedef struct vs. struct

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by bhdavis1978 View Post
    awesome!

    Thanks tabstop! What is the difference between what I had done, and what you showed? How does that differ from
    Code:
    typedef struct node
    {
    int data;
    int depth;
    struct node *parent_ptr;
    struct node *left_ptr;
    struct node *right_ptr;
    };
    What's different is that this struct has a name and can be referred to. It is legal to do something like
    Code:
    struct
    {
    int data;
    float other_data;
    };
    and create a new type. You can not do anything with that type, since it doesn't have a name: you can't refer to it to create a variable, etc. You had then taken that anonymous type and typedef'ed it to give it the name "node" (NOT "struct node").

    (ETA: So those pointers in your original type did not refer to the type you were in the process of defining, since the type you were in the process of defining had no name and could not be referred to.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LDAP Query
    By Travoiz in forum C++ Programming
    Replies: 0
    Last Post: 08-13-2009, 03:58 PM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Initialization from incompatible pointer type
    By Jailan in forum C Programming
    Replies: 9
    Last Post: 10-28-2007, 10:17 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

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