structure members outside main

This is a discussion on structure members outside main within the C Programming forums, part of the General Programming Boards category; ive been trying to solve this now for at least 2 hours. Code: struct sizes { int e_size; }; struct ...

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    structure members outside main

    ive been trying to solve this now for at least 2 hours.

    Code:
    struct sizes
    {
        int e_size;
    };
    
    struct sizes headersizes;
    
    headersizes.e_size = sizeof(struct ethhdr);
    
    ....
    int main...
    gcc tells me that there is a parse error before '.'
    in this line
    Code:
    headersizes.e_size = sizeof(struct ethhdr);
    why is this happening? cant i declare structure members as globals? or outside of a function?

    thanks

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You can declare a structure-variable outside a function, but you can not initialise a structure-variable outside a function in this way. But you can initialise a structure-variable during declaring, the same way you would do with other type of variables.

    Code:
    // This would initialise the struct member e_size to 1
    struct sizes headersizes = {1};
    Last edited by Shiro; 03-09-2003 at 06:29 AM.

  3. #3
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    in what way can i initialise it?
    or isnt it possible?

  4. #4
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I edited my post above just before your second post.

  5. #5
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    thanks

    i appreciate your help very much! little help for big project

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    8
    struct sizes
    {
    int e_size;
    };

    struct sizes headersizes = {(int)sizeof(struct ethhdr)};

    ...
    int main...


    ****Edit****
    Looks like you sorted it out while I was posting.
    Last edited by Heraclitus; 03-09-2003 at 06:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. double pointer to a structure
    By rohit_second in forum C Programming
    Replies: 5
    Last Post: 11-25-2008, 03:32 AM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 06:54 AM
  4. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 09:46 PM
  5. void main
    By Shadow in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 05-29-2002, 07:08 PM

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