doubts related to structure

Discussion in 'C Programming' started by somenath, Feb 16, 2007.

  1. somenath

    somenath Guest

    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
     
    somenath, Feb 16, 2007
    #1
    1. Advertising

  2. somenath

    Ian Collins Guest

    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.
     
    Ian Collins, Feb 16, 2007
    #2
    1. Advertising

  3. somenath

    Jack Klein Guest

    On 15 Feb 2007 18:45:42 -0800, "somenath" <>
    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/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Feb 16, 2007
    #3
  4. somenath

    ¬a\\/b Guest

    On 15 Feb 2007 18:45:42 -0800, "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 .


    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;
    }
     
    ¬a\\/b, Feb 16, 2007
    #4
  5. somenath

    santosh Guest

    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.
     
    santosh, Feb 16, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maxwell Hammer
    Replies:
    7
    Views:
    658
    Peter Hansen
    Jun 18, 2005
  2. Ameya

    doubts related to disk files in C

    Ameya, Nov 29, 2004, in forum: C Programming
    Replies:
    3
    Views:
    414
    Randy Howard
    Nov 30, 2004
  3. Lalatendu Das

    querry related to structure padding

    Lalatendu Das, Mar 19, 2006, in forum: C Programming
    Replies:
    20
    Views:
    629
    Chloe Johnson
    Mar 21, 2006
  4. the_init

    Error related to Structure or Union

    the_init, Mar 22, 2007, in forum: C Programming
    Replies:
    2
    Views:
    303
    Richard Bos
    Mar 22, 2007
  5. srinivasan srinivas

    Doubts related to subprocess.Popen()

    srinivasan srinivas, Jan 20, 2009, in forum: Python
    Replies:
    6
    Views:
    2,368
    Mark Wooding
    Jan 20, 2009
Loading...

Share This Page