praveen said:
-------------------------------My dud
program------------------------------------------
#include <stdioi.h>
I wish, I *wish*, I *REALLY* *WISH* that when people
post "this is my code" they would actually post their code
and not some half-baked, typo-ridden approximation! You're
just introducing extra bugs that divert attention from the
problem that's actually bothering you; is that wise?
#include <stddef.h>
typedef struct dummy{
int x, y, z;
}point;
int main() {
point a = {1, 2, 3};
int *p = (int *)(&a + offsetof(point, y);
Syntax error. If corrected, probably undefined behavior.
int *q = (int *)(&a + offsetof(point, z);
Syntax error. If corrected, definitely undefined behavior.
(No, I won't explain why one is "probably" the other "definitely,"
because I'm angry with you. See above.)
Definitely undefined behavior (twice, or possibly thrice).
(No, I won't explain the "thrice." See above.)
No, never.
i ran this program on DevC++ 4.9.9.2 and i'm getting some garbage
values as output!!!
You mean, after the required compiler diagnostics about
the syntax errors and the (probably) non-existent header?
If you ever expect to become a programmer, you *must* learn
how to ask questions without distorting the facts!
<Relenting> You have overlooked a basic fact about pointer
arithmetic: Adding an integer to a pointer "steps" it in whole
units of the pointed-to type. Now, ask yourself the question:
"What is the type of the pointer value `&a'?"