Question on Structures in C - a different one

This is a discussion on Question on Structures in C - a different one within the C Programming forums, part of the General Programming Boards category; I have executed the following program Code: #include <stdio.h> struct test { int a,b,c; }test; struct test testObj; main() { ...

  1. #1
    gvs
    gvs is offline
    Registered User
    Join Date
    Feb 2005
    Posts
    1

    Question on Structures in C - a different one

    I have executed the following program

    Code:
    #include <stdio.h>
    struct test
    {
         int a,b,c;
    }test;
    struct test testObj;
    
    main()
    {
        testObj.a = 100;
        testObj.b = 200;
        testObj.c = 300;
        printf("%d %d %d \n", testObj, testObj, testObj);
        return 0;
    }
    The output was - 100 200 300.

    I was expecting ouput as - 100 100 100.

    Can anyone explain why the output is like this?
    I tried looking into the assembly code for the same program and I found that before a call to printf the elements are pushed into the stack as following
    testobj+8
    testobj+4
    testobj

    When I replaced the printf statement with the following one
    Code:
        printf("%d %d %d \n", testObj, testObj, testObj.b);
    still I got the same o/p (even the assembly was also the same).

    Is it related to the compiler?
    Pointers please.....


    Gvs

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    No, it's related to your code being wrong.

    %d expects an integer (testObj.a) not a copy of a structure (testObj)

    That it apparently prints anything remotely sensible is luck, not judgement.

    I suggest you get a copy of gcc (or one of it's wrapper IDEs like DEV-C++) and use the -Wall compiler option to debug your mis-use of printf.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    i run the codes get the same results
    can you tell why ???

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try reading once in a while, and stop with the nonsensical posting.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    ---
    Join Date
    May 2004
    Posts
    1,379
    Quote Originally Posted by pacific
    i run the codes get the same results
    can you tell why ???
    because
    Code:
    printf("%d %d %d \n", testObj, testObj, testObj);
    every parameter passed to printf, even though they are not ints as they should be, the same 4 bytes (or sizeof(int)) is being printed from the begining of the structure.

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Don't waste your time sand_man , he's just posting to raise his post count.

    a question with pointers
    help with using printf()
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Design layer question
    By mdoland in forum C# Programming
    Replies: 0
    Last Post: 10-19-2007, 04:22 AM
  2. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  3. Replies: 4
    Last Post: 02-02-2003, 04:45 AM
  4. Question about structures
    By Randoon in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 10:47 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21