Thread: referencing a structure element from another structure

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    referencing a structure element from another structure

    Hello,

    This is a working peice of code but couldn't explain how it woks.

    Code:
    struct stA
    {
        ...
    
        int data;
        ...
        
    } ;
    
    struct stB
    {
        ...
    
        int fields;
        struct stA *stA_ptr;
    
        ...
    };
    
    main()
    {
        struct stB *stB_ptr;
    
       // i could see a statement like
    
       stB_ptr->stA_ptr[8].data=1500;     
      
       ...
    }
    I'v 2 questions at this point

    stA_ptr is just a pointer to structure stA.

    1. Then what does stA_ptr[8] imply ?

    2. How is it possible to access the element "data" in stA using stA_ptr[8].data ?
    I hope "stA_ptr[8]->data" should be the correct form but still its working.

    Hope to get a reply soon

  2. #2
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    i think if you declared an array of stA struct objects...
    then stA->ptr[8] would be a pointer to the last element, say, if the array size was 9
    .. i think
    You ended that sentence with a preposition...Bastard!

  3. #3
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    1. 9th element
    2. because stA_ptr is allocated as an array of stA

    cmiiw
    Do not stare at my avatar.

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    Quote Originally Posted by cph View Post
    1. 9th element
    2. because stA_ptr is allocated as an array of stA

    cmiiw
    :S i'm confused..if the size of an array is 9 then last index is SIZE-1..in my example that is why I said 8..

    i don't know about number 2. All i can say (guess) is stA_ptr references stA struct object.array or otherwise
    You ended that sentence with a preposition...Bastard!

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thanks Eman & cph.

    cph,

    Can you plz give further explanation on my 2nd question. I'm not able to follow.

  6. #6
    Registered User cph's Avatar
    Join Date
    Sep 2008
    Location
    Indonesia
    Posts
    86
    Quote Originally Posted by ajitht1986 View Post
    Thanks Eman & cph.

    cph,

    Can you plz give further explanation on my 2nd question. I'm not able to follow.
    Code:
    size_t max = 9; /* at least */
    
    stB_ptr->stA_ptr = malloc(max * sizeof(*(stB_ptr->stA_ptr)));
    
    if (stB_ptr->stA_ptr != NULL) {
        printf("Bam! and you have an array of...what?\n");
    
        /* do something */
        /* something like this?... */
        stB_ptr->stA_ptr[8].data=1500; 
    
        free(stB_ptr->stA_ptr);
    }
    probably like that, pcmiiw (p stands for please )
    Do not stare at my avatar.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adreess of pointed structure element
    By limp in forum C Programming
    Replies: 1
    Last Post: 06-12-2009, 05:54 AM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  4. Dynamic Data Structure -- Which one is better?
    By Yin in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2002, 11:38 PM
  5. referencing C structure elements in assembly
    By BigAl in forum C Programming
    Replies: 7
    Last Post: 09-10-2001, 03:19 PM

Tags for this Thread