Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > doubts related to structure

Reply
Thread Tools

doubts related to structure

 
 
somenath
Guest
Posts: n/a
 
      02-16-2007
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

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      02-16-2007
somenath wrote:
> 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.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Jack Klein
Guest
Posts: n/a
 
      02-16-2007
On 15 Feb 2007 18:45:42 -0800, "somenath" <(E-Mail Removed)>
wrote in comp.lang.c:

> 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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
 
Reply With Quote
 
¬a\\/b
Guest
Posts: n/a
 
      02-16-2007
On 15 Feb 2007 18:45:42 -0800, "somenath" <(E-Mail Removed)>
wrote:
>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;
}


 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      02-16-2007
somenath wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Doubts related to subprocess.Popen() srinivasan srinivas Python 6 01-20-2009 05:29 PM
How should multiple (related) projects be arranged (structured) and configured so that they can share code, have a related package structure and enable proper unittesting, and ensuring no namespace collisions ToddLMorgan@gmail.com Python 14 04-21-2006 04:03 PM
querry related to structure padding Lalatendu Das C Programming 20 03-21-2006 12:15 PM
How should threads be terminated? (related to 'Help with thread related tracebacks') Maxwell Hammer Python 7 06-18-2005 04:20 PM
doubts related to disk files in C Ameya C Programming 3 11-30-2004 04:56 AM



Advertisments