By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,187 Members | 1,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,187 IT Pros & Developers. It's quick & easy.

Get struct members address though a "pointer to struct"

P: 5
Hi,

Please have a look on the next code :

struct foo{
float b;
char z[5];
};
struct foo foo_init = {234.77,"xcdy"};
struct foo *prt_prt=&foo_init;

Q: How can I have access to the struct members in the same way as I got access to an array like:

int a[4] = {1,2,3,4,5}
int *p=&a[0]

printf ("address of a[1] is %p", (p+1))

??
Thanks
Dec 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: 34
declare a pointer and point it to a struct:

struct foo *f1;
struct foo f2;

f1 = &f2;
to access:
f1 -> b = 2.5;
f1 -> z[0] = 'f';

I'm guessing foo_init and prt_prt are global variables so accessing them would be the same as above:

prt_prt -> b to access the first member
prt_prt -> z to acces the array

or to put it another way (*prt_prt).b & (*prt_prt).z. Using the '->' operator is cleaner.
Dec 11 '07 #2

P: 5
Thanks for the answer but what i needed was something else:

( I don't know to explain exactly the problem, it might be impossibele though)

so:
I want to get the address of an struct member without knowing the actual name of the member. In the expresion *ptr->member (here I know the name) so I can get the address and the value very easely but how can I get this infos without knowing the name of the member?


Sorry for my poor language

Thanks in advance
Dec 11 '07 #3

100+
P: 147
Thanks for the answer but what i needed was something else:

( I don't know to explain exactly the problem, it might be impossibele though)

so:
I want to get the address of an struct member without knowing the actual name of the member. In the expresion *ptr->member (here I know the name) so I can get the address and the value very easely but how can I get this infos without knowing the name of the member?


Sorry for my poor language

Thanks in advance
structs are heterogenous, arrays are homogenous. So you need to know at least something about the layout of the struct in order to access its elements.
If you don't know the name of a member, but you know that the struct has a layout like {float b; char z[5]} and you know that your environment uses 32-bit floats, you can do something like:
Expand|Select|Wrap|Line Numbers
  1. char getCharFromStruct(void* myStruct, int index)
  2. {
  3.   char* ch = ((char*)myStruct) + 4;  //skip the float
  4.   ch += index;  //find z[index];
  5.   return *ch;
  6. }
  7.  
But that is not very clean programming and 99.9% of the time there's no good reason to do something like that and many good reasons not to.
Dec 11 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
But that is not very clean programming and 99.9%
And... it may not work at all due to compiler alignment of struct members with word boundaries.

I suggest you research how to set up a thunk table since that sounds like what you want.
Dec 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.