Thread: Nodes

  1. #1
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6

    Nodes

    I'm currently trying to figure out how to assign a node found in another struct to a variable.

    I intend to do something like
    Code:
    int access(struct randomstruct *point) {	struct node *trial;
            trial = malloc(sizeof(*trial));
     	trial = point->start;
    this is the struct
    Code:
    struct randomstruct {	/*codes*/
    	struct node *start; 
    };
    The compiler tells me trial and point->start are incompatible, but I'm quite sure point->start is of data type 'node' as well...
    How can I fix this?

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    It looks like it should work to me from what I see here. Can you paste more code so that we can try to replicate the issue more fully?

    Is it a runtime error, or compilation error? For example, this compiles and works for me:

    Code:
    struct node {
      int i; 
    };
    struct randomstruct {   /*codes*/
        struct node *start; 
    };
    
    int access(struct randomstruct *point) {    
      struct node *trial;
      trial = (struct node *)malloc( sizeof( struct randomstruct ) );
      trial = point->start;
      printf( "From access: %d\n", point->start->i );
      return 0; 
    }
    
    int main() {
      struct randomstruct *root = (randomstruct*)malloc( sizeof( struct randomstruct ) );
      root->start = (node*) malloc( sizeof( struct node ) ); 
      root->start->i = 10; 
    
      access( root ); 
    
      printf( "From main: %d\n", root->start->i );
      free( root ); 
    
      return 0;
    }
    Last edited by twomers; 11-28-2012 at 09:10 AM.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int access(struct randomstruct *point)
    {
        struct node *trial;
        trial = malloc(sizeof(*trial));
        trial = point->start;
    That last line is a memory leak BTW. You allocate memory to the pointer trial with the malloc call and then the very next line you throw that away when you reassign trial to point to something else.



    The first thing you should do before assigning point->start to trial is test that point is a valid (non-NULL) pointer. Then, only if that is true can you safely dereference point to get at start. The malloc call is probably unnecessary, unless start itself is NULL and you're attempting to assign it some memory.



    For the incompatible thing, do both of those bits of code have the same definition for node available? Where is node defined?
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by twomers View Post
    For example, this compiles and works for me:
    Sure?
    Code:
    $ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    foo.c: In function ‘access’:
    foo.c:11:16: warning: variable ‘trial’ set but not used [-Wunused-but-set-variable]
    foo.c: In function ‘main’:
    foo.c:19:32: error: ‘randomstruct’ undeclared (first use in this function)
    foo.c:19:32: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:19:45: error: expected expression before ‘)’ token
    foo.c:20:18: error: ‘node’ undeclared (first use in this function)
    foo.c:20:23: error: expected expression before ‘)’ token
    make: *** [foo] Error 1
    You are missing the struct keyword in your casts which are completely unnecessary. Haven't you copy'n'paste the code?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    Code:
    trial =(struct randomstruct *) malloc(sizeof(struct node)); 
    malloc() always return void pointer so you should type cast it in to your struct pointer.....
    Last edited by amzc; 11-28-2012 at 12:43 PM.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    32
    And one important thing is use typedef to save your coding time.....

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Quote Originally Posted by amzc View Post
    Code:
    trial =(struct randomstruct *) malloc(sizeof(struct node)); 
    malloc() always return void pointer so you should type cast it in to your struct pointer.....
    You should avoid casting the return value of the malloc call. The void pointer returned can be safely assigned to other pointer types without need for a cast. From a maintenance standpoint the malloc call for pointer trial (if it were to be used) should be as follows:
    Code:
    trial = malloc(sizeof(*trial));
    That way, if the type of variable trial changes then you don't need to go hunting down every sizeof(struct node) in your program to match what you just changed it to. Using sizeof(*trial) instead means you won't need to change a thing if/when the type of trial changes.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    6
    Quote Originally Posted by hk_mp5kpdw View Post
    For the incompatible thing, do both of those bits of code have the same definition for node available? Where is node defined?
    node is defined at a separate .h file together with randomstruct
    Code:
    struct node{
    struct daTag info;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nodes
    By Marcello in forum C++ Programming
    Replies: 8
    Last Post: 10-30-2011, 11:09 AM
  2. nodes
    By firefly in forum C++ Programming
    Replies: 8
    Last Post: 07-02-2005, 06:38 AM
  3. nodes
    By Zypo in forum C++ Programming
    Replies: 6
    Last Post: 11-08-2003, 04:56 PM
  4. First and Last Nodes
    By HybridM in forum C++ Programming
    Replies: 8
    Last Post: 09-27-2003, 03:47 AM
  5. Help w/nodes
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 08:09 PM

Tags for this Thread