static var inside struct definition

This is a discussion on static var inside struct definition within the C Programming forums, part of the General Programming Boards category; if i have a struct like this: Code: struct strInt { char vChar; static int vInt; } vInt is a ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    41

    static var inside struct definition

    if i have a struct like this:

    Code:
    struct strInt
    {
        char         vChar;
        static int  vInt;
    }
    vInt is a global variable but has the visiblity scope of strInt::vInt. And sizeof(strtInt) returns 1 'couse the allocated struct only contains vChar...is that true?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    It's true that each instance of the struct will contain only a vChar, but it's perhaps stretching it somewhat to assume that the size of the whole struct will always be 1. It should be 1 with a good compiler, but there are no guarantees.

    BTW, this syntax is of course illegal in a C program.
    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
    May 2007
    Posts
    41
    Quote Originally Posted by Salem View Post
    BTW, this syntax is of course illegal in a C program.
    ok for the compiler...but why is it illegal in C? it's c++?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, C++ supports "static" inside struct because struct and class are essentially the same thing in C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    If it was a C++ question, then you should have ask your question in the C++ section, not the C one. When you ask question in the C section, you'll have answer for the C programming language, not C++.

    So, like Salem said, declaring a static member in a struct is illegal in C.

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    41
    ok...then I'll simply use a global variable...thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  2. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM
  5. Header files
    By borland_man in forum C++ Programming
    Replies: 14
    Last Post: 02-22-2002, 03:30 AM

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