Understanding pointers in C

This is a discussion on Understanding pointers in C within the C Programming forums, part of the General Programming Boards category; I'm learning pointers in C and I'm developing a simple game for my class. I'm confused where it says return ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    4

    Understanding pointers in C

    I'm learning pointers in C and I'm developing a simple game for my class.

    I'm confused where it says return a pointer to the new node. Do I need to "return *node"? Why can't I just "return node"?

    Also, why is this procedure defined as "NODE *new_node(char *s)" and not "NODE new_node(char *s)" without the asterisk?

    Code:
    // This procedure should create a new NODE and copy
    // the contents of string s into the 
    // question_or_animal field.  It should also initialize
    // the left and right fields to NULL.
    // It should return a pointer to the new node
    
    
    NODE *new_node(char *s)
    {
      NODE new;
      strcpy(new->question_or_animal, s);
      new->left = NULL;
      new->right = NULL;
    
    
      return *new;
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't do either correctly here, because 'new' is a local variable, and I'm assuming that 'NODE' is not a typedefed pointer. As such, actually even if it was a typedef, it is still only a local variable, which is destroyed when your function returns. What you really need to do is:
    Code:
    NODE *new_node( char *s )
    {
        NODE *n = malloc( sizeof *n );
        if( n != NULL )
        {
            n->left = n->right = NULL;
            strcpy( n->question_or_animal, s ); // assuming qoa isn't just a pointer too
        }
        return n;
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    I should have mentioned that node is defined in a header file as

    Code:
    typedef struct node {
      char question_or_animal[200];
      struct node *left;
      struct node *right;
    } NODE;
    Does that change any of what you said?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    This is all a little speculative, since you didn't provide a definition of NODE, but like Quzah said, it seems to not be a typedef'ed pointer (probably just a struct).
    Quote Originally Posted by monmon_4 View Post
    I'm confused where it says return a pointer to the new node. Do I need to "return *node"? Why can't I just "return node"?
    Because you have to return something of the right type, and a NODE and NODE * are not the same type. One is a thing, the other is a pointer to that thing.
    Also, why is this procedure defined as "NODE *new_node(char *s)" and not "NODE new_node(char *s)" without the asterisk?
    If they returned the thing pointed to, instead of the pointer (address), you would return a copy of the NODE. That isn't a huge problem, but it isn't terribly efficient. As for why you shouldn't return the address of a local variable, Quzah already said that goes away and becomes invalid when the function exits. Most likely, the new_node function is supposed to allocate memory and pass back the address of what it allocated. If you returned a NODE instead of a NODE *, you would return a copy of what you just allocated, and then you would lose the address of the allocated memory, which would cause a memory leak.
    Code:
      return *new;
    You are asking for "the thing pointed to by new", but new isn't a pointer, so you can't dereference it (that's what the * does in this context). That doesn't even make sense. What you might have been thinking of is the & (address of) operator. But that would, as has been explained, be returning the address of a local variable, which is a big no-no.

    These may help your general pointer understanding:
    Pointers in C - Tutorial - Cprogramming.com
    Cprogramming.com FAQ > All about pointers
    Eternally Confuzzled - All About Pointers

    EDIT: I see you just posted the definition of NODE, and no, it doesn't change anything.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by monmon_4 View Post
    I'm learning pointers in C and I'm developing a simple game for my class.

    I'm confused where it says return a pointer to the new node. Do I need to "return *node"? Why can't I just "return node"?
    You could but that's not what's been asked of you apparently.
    Quote Originally Posted by monmon_4 View Post
    Also, why is this procedure defined as "NODE *new_node(char *s)" and not "NODE new_node(char *s)" without the asterisk?
    The * means that new_node() returns a pointer to NODE instead of an object of type NODE.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by monmon_4 View Post
    I should have mentioned that node is defined in a header file as

    Code:
    typedef struct node {
      char question_or_animal[200];
      struct node *left;
      struct node *right;
    } NODE;
    Does that change any of what you said?
    Nope!

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    4
    This mostly makes sense but doesn't 'NODE *n' have to point to something in your code? Shouldn't there an 'address-of' operator somewhere to denote where *n points to?

    Following from the above question, when you declare 'n->left' and 'n->right', there is nothing for it to assign to left/right because n is only a NODE pointer and not a NODE variable and it doesn't point to anything. Even if it did, wouldn't you need to say '*n->left' and '*n->right' respectively in order to dereference the pointer first?

    I'm probably wrong on most accounts here but I don't understand why.

    Quote Originally Posted by quzah View Post
    You can't do either correctly here, because 'new' is a local variable, and I'm assuming that 'NODE' is not a typedefed pointer. As such, actually even if it was a typedef, it is still only a local variable, which is destroyed when your function returns. What you really need to do is:
    Code:
    NODE *new_node( char *s )
    {
        NODE *n = malloc( sizeof *n );
        if( n != NULL )
        {
            n->left = n->right = NULL;
            strcpy( n->question_or_animal, s ); // assuming qoa isn't just a pointer too
        }
        return n;
    }

    Quzah.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,665
    "NODE *n;" is often described as declaring n as a pointer to NODE. An alternate description that might help you understand is that, equivalently, it declares *n to be a NODE (assuming n is initialised so it points at something valid).

    So, "NODE *n = malloc(sizeof *n)" tells the compiler to

    1) Create n as a pointer to NODE (so *n is a NODE, if n is initialised).

    2) Use malloc() to create enough memory to hold a NODE.

    3) Assign n to the address returned by malloc(). So *n is a reference to that memory.

    Since n is a pointer, the members of the node are accessed using -> operator, as in quzah's code. So n->left is the member named left in the struct referenced as (or named as, if you like) *n. n->left and (*n).left are equivalent (that is the relationship between the -> and . operators for structs).

    (*n)->left only makes sense to the compiler if *n is a pointer ... which it isn't in quzah's code, so using it would result in a compilation error.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding Pointers
    By Oonej in forum C Programming
    Replies: 23
    Last Post: 06-21-2011, 06:38 PM
  2. help understanding pointers and arrays
    By cyclone123 in forum C Programming
    Replies: 11
    Last Post: 02-03-2011, 02:11 PM
  3. Help with understanding Pointers...
    By kiddo in forum C Programming
    Replies: 12
    Last Post: 03-07-2010, 06:43 PM
  4. understanding pointers
    By princez90 in forum C Programming
    Replies: 14
    Last Post: 04-19-2008, 10:56 AM
  5. understanding pointers in my prog
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 03-22-2002, 12:04 PM

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