# Thread: referencing a structure element from another structure

1. ## 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. 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

3. 1. 9th element
2. because stA_ptr is allocated as an array of stA

cmiiw

4. Originally Posted by cph
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

5. Thanks Eman & cph.

cph,

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

6. Originally Posted by ajitht1986
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 )