is this a valid declaration?

This is a discussion on is this a valid declaration? within the C Programming forums, part of the General Programming Boards category; Hi, Please let me know how this a valid definition Code: typedef struct { int n[]; }franco; franco see; and ...

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

    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
    22,265
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 11: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, 07:39 AM

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