Thread: Question on Structures in C - a different one

  1. #1
    Registered User
    Join Date
    Feb 2005

    Question on Structures in C - a different one

    I have executed the following program

    #include <stdio.h>
    struct test
         int a,b,c;
    struct test testObj;
        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

    When I replaced the printf statement with the following one
        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.....


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    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.

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

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

    Hope is the first step on the road to disappointment.

  5. #5
    Join Date
    May 2004
    Quote Originally Posted by pacific
    i run the codes get the same results
    can you tell why ???
    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
    Don't waste your time sand_man , he's just posting to raise his post count.
    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, 05:45 AM
  4. Question about structures
    By Randoon in forum C Programming
    Replies: 2
    Last Post: 12-12-2002, 11:47 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM