pointer to a struct

This is a discussion on pointer to a struct within the C Programming forums, part of the General Programming Boards category; HI Ng. I got a littel problem with a pointer to a struct Code: struct Template { struct Template const ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13

    pointer to a struct

    HI Ng.

    I got a littel problem with a pointer to a struct
    Code:
    struct Template {
    struct Template const *NextMenu;
    };
    
    extern struct Template const MenuA;
    
    struct Template const *PointerMenuA[] ={&MenuA,0};
    
    struct Template const MenuA = {PointerMenuA[0]};
    In my array "*PointerMenu" I got all my addresses, on all my menus,
    in this example I only have one.

    I want to get the address on "MenuA" by accessing my struct "MenuA",
    but it seems impossible, I had thought of something like this, I
    know it's wrong, so I hope that you can help me.

    Code:
    void main()
    {
    struct Template const *Pointer;
    
    Pointer = &MenuA.NextMenu; // This is the line
    }
    I know that I can get the right address by doing this, but it is not good enough.

    Code:
    Pointer = PointerMenuA[0];
    I hope you understand my question, if not please leave a message, and I will try to explain it better
    Thank you in advance.
    Søren Panduro - Denmark

  2. #2
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Use -> when addressing attributes of pointers to structs where you would normally use .
    e.g.
    Code:
    foo *bar = malloc(sizeof(foo));
    
    bar->next = malloc(sizeof(foo));
    hope this helps.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13

    like this

    Hi
    If I do like this:
    Code:
    Pointer = &MenuA;
    Pointer = Pointer -> NextMenu;
    I still get the address of the "PointerMenuA" array?

    Any other ideas? Or maybe I just need a little more help, to see light

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Don't use void main().

    I want to get the address on "MenuA" by accessing my struct "MenuA",
    but it seems impossible
    Like this?
    Code:
    struct Template *pointer = &MenuA;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13
    Like this?
    Code:
    struct Template *pointer = &MenuA;
    Yes that right, but I have more than one address in my array, that’s why I have to access the address of my menus through the array.

    Something like that: (still wrong)

    Code:
    void main()
    {
    struct Template const *Pointer;
    
    Pointer = &MenuA.NextMenu; // This is the line
    }

  6. #6
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    Code:
    &MenuA.NextMenu
    is the address of the "NextMenu" member, not the address stored there. Is this what you want? If you're looking for the address of the object referenced by MenuA.NextMenu, simply use
    Code:
    MenuA.NextMenu
    (note the lack of an indirection operator ("&").
    Insert obnoxious but pithy remark here

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    i guess this is what u wanted

    Code:
    #include<stdio.h>
    
    struct something
    {
        int data;
        struct something *some;
    };
    
    int main()
    {
        struct something dosome;
        struct something *pointer;
        
        pointer = malloc(sizeof(struct something) * 2);
        dosome.data = 10;
        
        (pointer+1)->some=&dosome;
        
        printf("Address of dosome %p\n",&dosome);
        printf("Data in dosome structure %d\n",dosome.data);
        printf("The pointer structure %p\n",(pointer+1)->some);
        printf("The pointer having the same data %d",(pointer+1)->some->data);
        getchar();
        return 0;
    }
    
    /*my output
    Address of dosome 0022FF70
    Data in dosome structure 10
    The pointer structure 0022FF70
    The pointer having the same data 10
    */
    ssharish2005

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Hmm. I'd been fishing with something like this.
    Code:
    #include <stdio.h>
    
    struct Menu
    {
       const char         *title;
       struct Menu const  *next;
    };
    
    extern const struct Menu A, B, C, D;
    
    const struct Menu *const MenuList[] = { &A, &B, &C, &D };
    
    const struct Menu A = { "A", &D};
    const struct Menu B = { "B", &C};
    const struct Menu C = { "C", NULL};
    const struct Menu D = { "D", &B};
    
    int main(void)
    {
       const  struct Menu *menu = MenuList[0]->next;
       while ( menu )
       {
          puts(menu->title);
          menu = menu->next;
       }
       return 0;
    }
    
    /* my output
    D
    B
    C
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13
    Hi.

    I think we are on to something now , but my array is in my struct.

    Like this
    Code:
    struct Template const MenuA = {PointerMenuA[0]};
    And I can only access it through my struct.

    In my pointer I want the address, of the menu in my array, by accessing my struct.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13
    I got it!!!!!!!!!!

    My *NextMenu has to be a pointer to a pointer.

    Code:
    extern struct Template const MenuA;
    
    struct Template const **PointerMenuA[] ={&MenuA,0};
    struct Template const MenuA = {&PointerMenuA[0]};
    
    
    void mani()
    {
    
    pointer  = MenuA.NextMenu[0]
    
    }
    yes and thanks..

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ramdal
    I got it!!!!!!!!!!

    My *NextMenu has to be a pointer to a pointer.

    Code:
    void mani()
    {
    yes and thanks..
    Also, while mani is fine to be set to have a void return type, main is not a void function. main always returns an int.


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

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Just curious, do you have a more complete snippet? I ask because what I've pieced together from your responses would not appear to be correct:
    Code:
    struct Template {
    struct Template const *NextMenu;
    };
    
    extern struct Template const MenuA;
    
    struct Template const **PointerMenuA[] = {&MenuA,0};
    /*
    test.c 7: [64 Error] Type mismatch (initialization) (const struct Template ** = const struct Template *)
    */
    struct Template const MenuA = {&PointerMenuA[0]};
    /*
    test.c 11: [64 Error] Type mismatch (initialization) (const struct Template * = const struct Template ***)
    */
    or
    Code:
    struct Template {
    struct Template const **NextMenu;
    };
    
    extern struct Template const MenuA;
    
    struct Template const **PointerMenuA[] = {&MenuA,0};
    /*
    test.c 7: [64 Error] Type mismatch (initialization) (const struct Template ** = const struct Template *)
    */
    struct Template const MenuA = {&PointerMenuA[0]};
    /*
    test.c 11: [64 Error] Type mismatch (initialization) (const struct Template ** = const struct Template ***)
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Registered User
    Join Date
    Dec 2005
    Location
    Denmark
    Posts
    13
    Hi

    it has to be like this :

    Code:
    struct Template {
    struct Template const **NextMenu;
    };
    
    extern struct Template const MenuA;
    
    struct Template const *PointerMenuA[] = {&MenuA,0};
    
    struct Template const MenuA = {&PointerMenuA[0]};

    Q: whar editor/complier do you use, I use one for 8-bit Zilog and I use Zilog uC, or what can you recommend?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Ah:
    Code:
    struct Template const *PointerMenuA[] = {&MenuA,0};
    Looks fine. Thanks.

    Quote Originally Posted by ramdal
    Q: whar editor/complier do you use, I use one for 8-bit Zilog and I use Zilog uC, or what can you recommend?
    I use SlickEdit and a variety of compilers. The diagnostic messages in my previous post were the output of PC-lint. And I haven't done Zilog in quite a number of years, so I have no recommendations there.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

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

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