Thread: is this a valid declaration?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    145

    is this a valid declaration?

    Hi,

    Please let me know how this a valid definition
    Code:
    typedef struct
    {
    	int n[];
    }franco;
    franco see;
     and 
    printf ("sizeof:%d",sizeof(see));
    
    gives Result : 4
    and this is not
    Code:
    main()
    {
      int a[];
    }
    How is the memory allocated when franco is instantiated.
    Thanks to all

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sanddune008
    Please let me know how this a valid definition
    The MinGW port of gcc 3.4.5 reports an error that a flexible array member is declared in an otherwise empty struct.

    The C standard makes it clear that the syntax denotes an incomplete type which can be "completed, for an identifier of that type, by specifying the size in a later declaration (with internal or external linkage).". This explains why your latter example does not compile.

    Back to your original question: even if you do add another member variable to make my compiler happy, I am not sure how to interpret this "flexible array member", actually.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Sort of. The purpose of a declaration like you do in the struct is that you allocate memory dynamically to match the size of the actual space needed. So whilst the declaration is valid, the way you use it isn't quite what the standard suggests.

    The second usage of empty brackets is definitely invalid.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM