C
Capstar
Hi NG,
Does the following code invoke undefined behaviour? I compiled it and it
runs fine. I think this is fine because I think the last 3 arguments of
struct a are organised in memory the same way as the 3 arguments of
struct b. But if my previous statement isn't true in all cases or on all
platforms or with all compilers, this probably will invoke undefined
behaviour.
Can anyone tell me if I'm right or not?
Thanks in advance,
Mark
#include <stdio.h>
struct a
{
int a;
int b;
char *foo;
long int c;
};
struct b
{
int b;
char *foo;
long int c;
};
int main(void)
{
struct a foo;
struct b *bar;
foo.a = 1;
foo.b = 2;
foo.c = 3;
bar = (struct b*)&foo.b;
bar->b *= bar->c;
bar->foo = "This is a test\n";
printf("foo.a: %d\nfoo.b: %d\nfoo.c: %ld\nfoo.foo: %s\n", foo.a,
foo.b, foo.c, foo.foo);
return 0;
}
Does the following code invoke undefined behaviour? I compiled it and it
runs fine. I think this is fine because I think the last 3 arguments of
struct a are organised in memory the same way as the 3 arguments of
struct b. But if my previous statement isn't true in all cases or on all
platforms or with all compilers, this probably will invoke undefined
behaviour.
Can anyone tell me if I'm right or not?
Thanks in advance,
Mark
#include <stdio.h>
struct a
{
int a;
int b;
char *foo;
long int c;
};
struct b
{
int b;
char *foo;
long int c;
};
int main(void)
{
struct a foo;
struct b *bar;
foo.a = 1;
foo.b = 2;
foo.c = 3;
bar = (struct b*)&foo.b;
bar->b *= bar->c;
bar->foo = "This is a test\n";
printf("foo.a: %d\nfoo.b: %d\nfoo.c: %ld\nfoo.foo: %s\n", foo.a,
foo.b, foo.c, foo.foo);
return 0;
}