doubts related to structure

S

somenath

Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70

Regards,
Somenath
 
I

Ian Collins

somenath said:
Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .

Probably at least 3 compiler warnings.

The output is undefined.
 
J

Jack Klein

Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .

There is no output defined by the C standard for the turdlet below.
There is no defined output because of the undefined behavior.
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;

If your compiler does not emit a diagnostic for the line above, it is
not a C compiler.
++dp ; ++dp ;

Each of the two statements above is undefined behavior.
printf("%x\n", dp->day);

The statement above has undefined behavior.
}
Out put is 70

Maybe the output is 70, maybe it reformats your hard drive, maybe it
makes the Sony battery in your laptop explode into flames. This is
invalid, and very stupid, code, and what it does or does not do has
nothing at all to do with the C language.
 
¬

¬a\\/b

Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .

here the output of "the modificated version" was something like

-----------------------------------
70
1
1
girono=1
mese =1
anno =2006
-----------------------------------
girono dp1=1
mese dp1=1
anno dp1=2006

because ++dp; jump sizeof(MYDATE)==here to 3 ints
but all this is UB (the output dipends from how compiler dispose the
data)
thats UB so if someone run it he/she shold know all can happen
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70

Regards,
Somenath


#include <stdio.h>

typedef struct
{ int day;
int month;
int year;
} mydate;

int x = 0x10;
int y = 0x20;
int z = 0x30;

int a = 0x40;
int b = 0x50;
int c = 0x60;

int d = 0x70;

mydate abc = { 1, 1, 2006 };

int main(void)
{mydate *dp = &x, *dp1=&y;

if(sizeof(mydate)!= 3*sizeof(int) || &x+7!=&abc)
{printf("I don't like padding\n");
return 0;
}
++dp; ++dp;
++dp1; ++dp1;

printf("%x\n", dp->day);
printf("%x\n", dp->month);
printf("%x\n", dp->year);

/* here "dp" point to &d */
/* here ((int*)dp + 1) point to &abc */

printf("girono=%d \n", ((mydate *)( (int*)dp + 1))->day);
printf("mese =%d \n", ((mydate *)( (int*)dp + 1))->month);
printf("anno =%d \n", ((mydate *)( (int*)dp + 1))->year);

printf("girono dp1=%d \n", dp1->day);
printf("mese dp1=%d \n", dp1->month);
printf("anno dp1=%d \n", dp1->year);

return 0;
}
 
S

santosh

somenath said:
Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70

Here're the diagnostics my compiler, (gcc), emits when I compile your
code above, with a minor addition, (#include <stdio.h>).

gcc -Wall -Wextra -ansi -pedantic -O4 -o 02 02.c
02.c:21: warning: return type defaults to int
02.c: In function main:
02.c:22: warning: initialization from incompatible pointer type
02.c:25: warning: control reaches end of non-void function

The third warning indicates the first place where you invoke undefined
behaviour by initialising dp, which is a pointer to type MYDATE with a
pointer value of a different type, specifically int. Once you invoke
undefined behaviour, as defined by the C standard, the subsequent
behaviour of the program can be anything.

The two increments of the pointer dp also result in undefined
behaviour, as does the next printf() statement.

Just to illustrate that invoking or relying on undefined behaviour is
foolish, the output I get when I run this program is:
0

You cannot make any assumptions about the physical layout of objects
in C. The standard specifies some of their properties like type, size,
scope and linkage, but not their relative placements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top