Thread: struct pointer to struct pointer..??

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    Question struct pointer to struct pointer..??

    I am experiencing problems with a program that involves alot of structures and pointers, i think i have narrowed my problem down to an issue with ether assigning or referencing struct memebers...

    if I have:
    Code:
    struct ONE{
    
       struct TWO* two_pointer;
    
    };
    
    struct TWO{
    
       int value;
    };
    if im in a situation where i have to fill the memeber value of struct TWO with x, but i can only directly acces struct ONE, is this not the proper procedure..?

    Code:
    struct ONE* one_p=(struct ONE*)malloc(sizeof(struct ONE));
    struct TWO* one_p->two_pointer=(struct TWO*)malloc(sizeof(struct TWO));
    
    one_p->two_p->value=x;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're still compiling as C++ if you have to typecast malloc to stifle warnings.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    int main( void )
    {
        struct foo
        {
            int x;
        };
        struct bar
        {
            struct foo *y;
        } *baz;
    
        if( (baz = malloc( sizeof *baz )) )
        {
            if( (baz->y = malloc( sizeof *(baz->y) )) )
            {
                baz->y->x = 5;
                printf( "%d\n", baz->y->x );
                free( baz->y );
            }
            free( baz );
        }
    
        return 0;
    }
    I find it always best to make a small compilable example.


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

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    Day #8 now of trying to figure this out...im debating on giving up programming for good.....ive tried to rewrite this program 101 ways now,including your solution quzah and im still in the same place i was last week...when i tried to write it the memory allcoation part liek you told me to
    Code:
    baz = malloc( sizeof *baz ))
    it tells me that i cant cast from void* to "bar"*
    also what im noticing is that i have split everything up into different modules

    construct(pointer)...allocates memory
    initialize(pointer)...fills with values
    use(pointer)....use it
    destruct(pointer)...free memory

    whats weird is that it appears as if the program is jsut not calling or not entering construct_pointer()..ill place breakpoints at lines of code inside the construct() function,but when i start the debugger, the breakpoints are removed and replaced with warnings saying "The breakpoint will not be hit. No Excecutable code is associated with this line"...anyone know why thats happening?

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    heres the exact flow control of my program (simplified)..hopefully someone can see what i am doing wrong..
    Code:
    struct Gemstone* gem;
    main(){
    
    construct_gem(gem);
    initialize_gem(gem);
    use_gem(gem);
    destruct(gem);
    }
    
    construct_gem(struct Gemstone* gem_p){
    
    	gem_p=(struct Gemstone*)malloc(sizeof(struct Gemstone));
    	gem_p->position=(struct Point*)malloc(sizeof(struct Point));
    	gem_p->position->x=0;
    	gem_p->position->y=0;
    	gem_p->size=0.5;
    	gem_p->value=10;
    
          //none of this code seems to be excecuted
    
    }
    initialize_gem(struct Gemston* gem_p){
    
       gem_p->postion->x=-0.77
       gem_p->position->y=0.93
      //more code that is irrelevant
    
    }
    
    use_gem(struct Gemstone* gem_p){
    }
    destruct_gem(struct Gemstone* gem_p(struct Gemstone* gem_p)

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! it is the proper procedure with minor changes to the line shown below, hope that will fix your issue:
    Code:
    struct TWO* one_p->two_pointer=(struct TWO*)malloc(sizeof(struct TWO));
    should be written as
    Code:
    one_p->two_pointer=(struct TWO*)malloc(sizeof(struct TWO));

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    that actualy is how i worte it in the code, when i wrote it above, i accidently put in the extrat struct TWO*...and if that is the proper procedure, then cleary the problem is somewhere else..

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by paret View Post
    Code:
    baz = malloc( sizeof *baz ))
    it tells me that i cant cast from void* to "bar"*
    You either aren't including the right header, or you are compiling as C++.
    Quote Originally Posted by paret View Post
    construct(pointer)...allocates memory
    Won't work unless you're assigning a return value, or are using a pointer to a pointer.
    Code:
    void foo( type *bar )
    {
        bar = malloc( ... ); /* no */
    }
    
    void foo( type **bar )
    {
        *bar = malloc( ... ); /* yes */
    }
    
    type *foo( ... )
    {
        type *p;
        p = malloc( ... );
        ...
        return p; /* yes */
    }

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

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    your rightt!....that makes sense...thanks man!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. pointer problem or so...
    By TL62 in forum C Programming
    Replies: 19
    Last Post: 01-12-2008, 11:45 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM