Thread: Assigning pointers from pointers in struct.

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    Assigning pointers from pointers in struct.

    How can I assign a location to a pointer from a pointer within a struct.

    if n->left points to "node a;"
    how can I make node pointer ptr point to node a?

    if you get my drift.

    also if i have made a node.
    Code:
    node nn;
    nn.val = 5;
    how can i make an existing pointer within a node
    which i have a pointer to point to the new node
    Code:
    int blah (node *mega)
    {
      node nn;
      nn.val = 5;
      mega->left = nn;
      return 0;
    }
    doesnt seem to work. Or is this because the function is destroying the new node nn when it finishes?
    Last edited by Brian; 10-18-2003 at 04:28 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you wouldn't want to do

    node nn;
    nn.val = 5;
    mega->left = nn;

    Because you're pointing at a local

    But if you did, the syntax would be
    mega->left = &nn;


    This is more usual
    node *nn = malloc( sizeof(*nn) );
    nn->val = 5;
    mega->left = nn;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Brilliant, thanks .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By brb9412 in forum C Programming
    Replies: 16
    Last Post: 01-04-2009, 04:05 PM
  2. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Help please im stuck
    By ItsMeHere in forum C Programming
    Replies: 7
    Last Post: 06-15-2006, 04:07 AM
  5. Array of struct pointers - Losing my mind
    By drucillica in forum C Programming
    Replies: 5
    Last Post: 11-12-2005, 11:50 PM