Thread: what does a structure variable mean?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Lightbulb what does a structure variable mean?

    Hello,

    The following is a sample that I've tried...
    Code:
    struct st
    {
         char ch[4];
         int i;
    };
    
    int main()
    {
         struct st mystruct;
         mystruct.ch[0] = 'A';     // Ascii value 0x41 
         mystruct.ch[1] = 'B';     // Ascii value 0x42
         mystruct.ch[2] = 'C';     // Ascii value 0x43
         mystruct.ch[3] = 'D';     // Ascii value 0x44
         mystruct.i = 97;
    
         printf("Type 1: 0x%x\n",mystruct);
         printf("Type 2: %c\n",mystruct);
         return 0;
    }
    And the output is:
    Code:
    Type 1: 0x44434241
    Type 2: A
    I've used gcc 3.1 compiler to compile it on Redhat Linux 8.0 (cant change any of them )
    My Question
    What type will a structure variable assume? As can be seen from above code, if I print the structure variable as an int using %x, it prints the first 4 bytes and if I print it as a character, it prints only the first byte.
    So, how is it assuming it all? Or, is it just an Integer and since i've used %c, it is getting type-casted (internally or by printf) and getting printed??
    Last edited by vsriharsha; 08-26-2004 at 09:37 AM. Reason: Type error
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. pthread question how would I init this data structure?
    By mr_coffee in forum C Programming
    Replies: 2
    Last Post: 02-23-2009, 12:42 PM
  3. Replies: 5
    Last Post: 02-14-2006, 09:04 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Variable is not a structure and Sort char **
    By ChazWest in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 09:40 AM