Thread: Declaration problem

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    6

    Declaration problem

    We're having trouble with some C code here.

    Basically, the file 'type.c' contains a lot of static structures,
    generated by a script (reading a data file). Some of the structures
    contain pointers to other structures in the file and rather than deal
    with the intense (and possibly impossible) complexity of making sure all
    the structures are declared in the right order to satisfy dependencies,
    the script just puts a list of forward declarations at the top of the
    file and then just writes the actual declarations in whatever order it
    receives them from the original data file. The compiler is then expected
    to do the right thing (as it does with function prototypes).

    The declarations that generate errors look like:

    Code:
    static const struct mkf_type *pretypes_objfile[];
    The sort of errors we get are:

    Code:
    type.c:24: error: array size missing in 'pretypes_objfile'
    We don't see these errors if compiler warnings are suppressed, but
    that's not really good enough as it suggests the code isn't correct
    ANSI C89 (we're trying to stick to C89 as C90 support is STILL sketchy
    amongst various compilers - what year is this?).

    Now correct me if I'm wrong, but since when was "struct something *var[];"
    illegal in C?

    The structure is declared later on as:

    Code:
    static const struct mkf_type *pretypes_objfile[] = {
      &type_csource,
      &type_cxxsource,
    };
    Bare in mind that (under gcc), this code actually does compile if the
    -pedantic flag isn't used on the command line.

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I'm not really a C 'bookworm' or tOo sure of what you are doing, but the clue is in the error
    is it not? you have an array sized zero and apparently you are putting something in in?
    Why don't you do the sensible thing and make the array size one?

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That would not be sensible.

    What happens if you use **pretypes_objfile instead of *pretypes_objfile[]?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by dwks
    That would not be sensible.

    What happens if you use **pretypes_objfile instead of *pretypes_objfile[]?
    Dunno, I don't write code like that in the first place

    At a guess I would say nothing much, you have just made it an array pointer.

    As the Irish would say when asked for directions, "You would be better off not starting from here!".
    Last edited by esbo; 08-31-2006 at 01:30 PM.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by dwks
    What happens if you use **pretypes_objfile instead of *pretypes_objfile[]?
    Well actually that seems to cause further problems as certain things stop being
    constant initializers:

    Code:
    static const struct mkf_type **pretypes_csource = {
      &type_compiletx,
    };
    static const char *suffixes_csource[] = {
      "c",
    };
    static const struct mkf_type type_csource = {
      &type_objfile,
      pretypes_csource,
      sizeof(pretypes_csource) / sizeof(struct mkf_type *),
      suffixes_csource,
      sizeof(suffixes_csource) / sizeof(const char *),
      0,
      0,
      0,
      MKFF_CC,
      0,
    };
    Gives the error:

    Code:
    type.c:37: warning: initialization from incompatible pointer type
    type.c:44: error: initializer element is not constant
    type.c:44: error: (near initialization for `type_csource.pre_types')
    Which just seems wrong to me somehow...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Now correct me if I'm wrong, but since when was "struct something *var[];" illegal in C?
    Since always, IIRC. If you used extern instead of static with that declaration then it would be a declaration of an incomplete type, which is fine, and probably what you're thinking of. But there's no such thing as an array without a size, and that's what you're currently trying to define.
    My best code is written with the delete key.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by xwasp
    Code:
    static const struct mkf_type *pretypes_objfile[];
    This is an attempted definition of an array without a specified size. Illegal. You need to make it a declaration.
    Typically, you create variable declarations with the extern keyword. I don't know how the compiler will react to the presence of both extern and static, though.
    Code:
    extern const struct mkf_type *pretypes_objfile[];
    
    /* ... */
    
    static const struct mkf_type *pretypes_objfile[] = {
      /* ... */
    };
    (we're trying to stick to C89 as C90 support is STILL sketchy
    amongst various compilers - what year is this?).
    C89 and C90 are the same. The ANSI standard came out in '89, the ISO in '90. You mean C99.

    Now correct me if I'm wrong, but since when was "struct something *var[];"
    illegal in C?
    It has always been.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Yeah I think that's probably the answer. Put a ton of extern declarations at the
    top of the file and remove the static keyword.

    I'll try it in a bit...

  9. #9
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by CornedBee
    C89 and C90 are the same. The ANSI standard came out in '89, the ISO in '90. You mean C99.
    Yeah that was a typo.

  10. #10
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Quote Originally Posted by CornedBee
    This is an attempted definition of an array without a specified size. Illegal. You need to make it a declaration.
    Typically, you create variable declarations with the extern keyword. I don't know how the compiler will react to the presence of both extern and static, though.
    That is incredibly annoying how gcc will compile the previous code without
    a warning if you don't use the -pedantic flag.

    Anyway, I'll try what you said.

  11. #11
    Registered User
    Join Date
    Aug 2006
    Posts
    6
    Well, it seemed to work.

    I'm sure I'll soon find out if mixing extern and static is going to cause portability
    problems!

    Thanks for the help.

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. yet another forward declaration problem.
    By sloppy_coder in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2005, 02:59 AM
  3. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. cannot start a parameter declaration
    By Dark Nemesis in forum C++ Programming
    Replies: 6
    Last Post: 09-23-2005, 02:09 PM