0

pointers to structure members

trying to understand why i would need a pointer to a structures member,doesn't the members identifier act as a pointer would already? i think it connects to a larger question ive been trying to figure out which is how identifiers of different data types point to their corresponding blocks of memory,it seems very similar to pointers, as they point to parts of memory. however no addresses are needed,so i dont really understand how identifiers work :) would really like some help figuring it out.

26th Aug 2019, 12:09 PM
Ami Sternlicht
Ami Sternlicht - avatar
5 ответов
+ 2
Moin! I don't fully get what you are asking, but from what I understand: An identifier is just the name given to a variable. The expression `foo->bar` contains two identifiers, `foo` and `bar`. A struct is a single block of memory and each member in the struct lives at some "offset" from the start of the block. But it's not really the same as a pointer. Consider this: struct some_struct { int foo; int bar; } struct some_struct *a; struct some_struct *b; The `bar` member probably lives 4 bytes after the start of the struct (The first 4 bytes are taken up by `foo`), but `a->bar` and `b->bar` obviously don't live at the same place in memory, otherwise they would be overwriting each other. You can have pointers to members like `&a->bar` and it's just a regular old pointer to an int like any other. I hope that helped somewhat.
26th Aug 2019, 12:44 PM
Schindlabua
Schindlabua - avatar
+ 1
thank you for answering! i realize the memory allocated for the "bar" member will be probably after the structure and that it will be pointing to an int. my question was why use a pointer to that member if i can use the members name which will point to the same block of code. i looked at the code again and noticed it was because a pointer will be usable in many different functions and can be assigned to different members each time! though i do not understand how a members name points to the block of code,does it store a hexadecimal address number like a pointer does?
26th Aug 2019, 6:04 PM
Ami Sternlicht
Ami Sternlicht - avatar
+ 1
Yep you got it. A pointer to an int is totally agnostic to whether whatever it points to is inside a struct or not, and you can reuse it etc. And, good question. We have to make a distinction between run-time and compile-time there. Our `struct some_struct` above will probably be 8 bytes big because it contains two 4-byte integers. And `foo` lives at offset 0 and `bar` probably lives at offset 4. All that is known to the compiler too. So whenever you write `a->bar` in code, the compiler can translate it to `a + 4 bytes`. So after compilation all the names of the members have disappeared and we don't have to keep track of anything! (That's why you can't add/remove fields of structs during runtime, it would mess up all the offsets)
26th Aug 2019, 6:22 PM
Schindlabua
Schindlabua - avatar
0
thank you again for answering! you really helped me work this through. i just dont see how a variables/members name gets compiled to a certain block of code. now i understand my question is about the compiling process and name binding i see there is much to learn. thank you for pointing me in the right direction
26th Aug 2019, 7:03 PM
Ami Sternlicht
Ami Sternlicht - avatar
0
Sure, feel free to hit me up in a private conversation if you need anything.
26th Aug 2019, 7:06 PM
Schindlabua
Schindlabua - avatar