My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
yes, you are right. Structs are used to keep hold a set of data that belongs together, like name, address data.
Will use array. This thread was informative very usefull nevertheless.
Edit:
@imalc: Many years experience not. I am picking up my old C program that I was creating, it partially works but something is wrong with the semaphores, and mutex. Time to rap things up and finish things. If I were thinking like you said, that it's the hardest thing, well, where should I go in the future? Just stay in bed and think everything is difficult? God gave us a brain to use. So I intend to.
It might be a good way to say positive and encouraging things, instead of saying: no it's too difficult, etc.
Personalities are shaped with what you say to others. Every word has a positive or negavtive charge to it.
Don't you agree? Now , let us write code iMalc!
Last edited by chihwahli; 03-22-2013 at 02:44 AM.
Depends on your definition of informative. Until AndiPersti suggested using an array, the thread was full of misinformation - or information that is not always true.
A basic problem with the advice given is that layout of structs is implementation dependent. One consequence of this is that incrementing a pointer to one member is not guaranteed to give the address of the next.
While it is true that struct members will appear in order, there is nothing preventing a compiler from inserting padding between them. So, the output of this code is 100% compiler dependent. It can vary between compilers from different vendors. It can change when you update your compiler.Code:#include <stdio.h> struct X { int a1; int a2; }; int main() { struct X x; int *p1 = &(x.a1); int *p2 = p1 + 1; if (p2 == &(x.a2)) printf("Worked\n"); else printf("Did not work\n"); return 0; }
Thanks Grumpy, you mean this:
what is structure padding - C / C++
There are ways to calculate it if I recall it right. Thanks for bringing it up. I might have missed it and guess what's wrong...
Sure, there are ways to calculate what padding is between struct members. But doesn't having to do that work against your wish to easily iterate over members? Arrays have none of that sort of problem and, unlike structs, are meant to be iterated over.