Can anyone explain this

This is a discussion on Can anyone explain this within the C Programming forums, part of the General Programming Boards category; Code: char g[10]; /*ten bytes*/ int main() { struct g { int a; }; printf("g is supposed to be (4)sizeof(a) ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    85

    Question Can anyone explain this

    Code:
    char g[10]; /*ten bytes*/
    
    int main()
    {
      struct g { 
                 int a;
               };
      
      printf("g is supposed to be (4)sizeof(a) %d",g);
      return 0;
    }
    Output display g = 10.
    Do you guys know why it output the sizeof g is
    equal to 10 instead of 4?
    This code was compiled with GCC compiler.
    Can anyone explain concisely about this??
    Thanx!
    P.S:
    As I know in C if the local variable is the same
    name with global variable then that name become
    locally access in the scope where it define.
    For ex:
    int a=1;
    void fun()
    { int a=10;
    int b;
    printf("a value should be (10): %d",a);
    }
    int main()
    {
    fun();
    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    struct g { 
                 int a;
               };
    This doesn't declare a struct, it only defines one.

    If you wanted to print the size of the struct, use
    >sizeof(struct g)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you explain these bitwise operations?
    By 6tr6tr in forum C++ Programming
    Replies: 6
    Last Post: 10-29-2008, 01:19 PM
  2. Please explain?
    By neo_phyte in forum C Programming
    Replies: 3
    Last Post: 08-25-2006, 05:23 AM
  3. Can someone explain to me what this code means
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 12-22-2002, 11:36 AM
  4. Replies: 4
    Last Post: 11-19-2002, 08:18 PM
  5. Can someone explain "extern" to me?
    By valar_king in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 12:22 AM

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