some strange for me

Discussion in 'C Programming' started by profjwang@gmail.com, Dec 21, 2007.

  1. Guest

    I have a structrue like this:
    struct object_t{
    struct object *next;
    char data[0];
    }
    and another one:
    struct bucket_t{
    int a;
    struct object objects[0];
    }

    but the VC compiler tell me have error occurred in struct object
    object[0], why??
     
    , Dec 21, 2007
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >I have a structrue like this:
    >struct object_t{
    > struct object *next;
    > char data[0];
    >}
    >and another one:
    >struct bucket_t{
    > int a;
    > struct object objects[0];
    >}


    >but the VC compiler tell me have error occurred in struct object
    >object[0], why??


    Direct constraint violation.


    C89 3.5.4.2 Array Declarators

    Constraints
    The expression delimited by [ and ] (which specifies the size of
    an array) shall be an integral constant expression that has a
    value greater than zero.

    --
    "Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us." -- Ecclesiastes
     
    Walter Roberson, Dec 21, 2007
    #2
    1. Advertising

  3. On Fri, 21 Dec 2007 09:39:15 -0800, profjwang wrote:

    > I have a structrue like this:
    > struct object_t{
    > struct object *next;
    > char data[0];
    > }
    > and another one:
    > struct bucket_t{
    > int a;
    > struct object objects[0];
    > }
    >
    > but the VC compiler tell me have error occurred in struct object
    > object[0], why??


    there is no definition of "struct object".
     
    Mark McIntyre, Dec 21, 2007
    #3
  4. writes:
    > I have a structrue like this:
    > struct object_t{
    > struct object *next;
    > char data[0];
    > }
    > and another one:
    > struct bucket_t{
    > int a;
    > struct object objects[0];
    > }
    >
    > but the VC compiler tell me have error occurred in struct object
    > object[0], why??


    I see at least two possible problems in the code you posted. One is
    that you declare "struct object_t" but refer to "struct object".
    Another is that you attempt to declare zero-sized arrays, which is not
    allowed in C.

    I suspect the code you posted is not the actual code you're having
    problems with, which makes it difficult to tell what your real problem
    might be. In addition, you tell us that your compiler tells you an
    error occurred, but you don't show us the actual error message.

    If you copy-and-paste (don't re-type) the *exact* code and the *exact*
    error message, we can be a lot more helpful.

    See <http://www.catb.org/~esr/faqs/smart-questions.html>.

    --
    Keith Thompson (The_Other_Keith) <>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 21, 2007
    #4
  5. santosh Guest

    wrote:

    > I have a structrue like this:
    > struct object_t{
    > struct object *next;
    > char data[0];
    > }
    > and another one:
    > struct bucket_t{
    > int a;
    > struct object objects[0];
    > }
    >
    > but the VC compiler tell me have error occurred in struct object
    > object[0], why??


    You have several typos there. Anyway, array declarations must consist of
    at least one element, and it must be a constant expression known at
    compile time. C99 has variable length arrays, but I don't know them
    enough to talk much about them. In any case, a possible alternative is:

    struct bucket_t {
    int a;
    struct object_t *objects;
    };

    This declares a pointer to struct object_t which you can initialise at
    runtime to point to one or more instances of object_t, through malloc.

    Static allocation is easy but not very flexible. Dynamic allocation
    demands more work and attention, and may involve a very small runtime
    cost, but is the most flexible scheme.
     
    santosh, Dec 21, 2007
    #5
  6. Guest

    Walter Roberson wrote:
    > In article <>,
    > <> wrote:
    > >I have a structrue like this:
    > >struct object_t{
    > > struct object *next;
    > > char data[0];
    > >}
    > >and another one:
    > >struct bucket_t{
    > > int a;
    > > struct object objects[0];
    > >}

    >
    > >but the VC compiler tell me have error occurred in struct object
    > >object[0], why??

    >
    > Direct constraint violation.
    >
    >
    > C89 3.5.4.2 Array Declarators
    >
    > Constraints
    > The expression delimited by [ and ] (which specifies the size of
    > an array) shall be an integral constant expression that has a
    > value greater than zero.


    True. Note, however, that some popular compilers have allowed a value
    of 0 as an extension, to implement the "struct hack" for dynamically
    allocating structures containing arrays of unspecified length. For
    compilers without that extension, a length of 1 would often work, even
    though it was not clear that the C89 standard allowed this. This
    concept has been officially adopted in C99 as a "flexible array"
    member, but the syntax is different: instead of using a length of
    either 0 or 1, the length is left out entirely.
     
    , Dec 21, 2007
    #6
  7. CBFalconer Guest

    wrote:
    >
    > I have a structrue like this:
    > struct object_t{
    > struct object *next;
    > char data[0];
    > }
    > and another one:
    > struct bucket_t{
    > int a;
    > struct object objects[0];
    > }
    >
    > but the VC compiler tell me have error occurred in struct object
    > object[0], why??


    0 length arrays are forbidden in C.

    --
    Merry Christmas, Happy Hanukah, Happy New Year
    Joyeux Noel, Bonne Annee.
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
     
    CBFalconer, Dec 21, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. moondaddy
    Replies:
    0
    Views:
    415
    moondaddy
    Oct 19, 2004
  2. davidw
    Replies:
    0
    Views:
    526
    davidw
    Feb 8, 2006
  3. fduch_nsk
    Replies:
    2
    Views:
    2,728
    fduch_nsk
    Jun 14, 2005
  4. DeltaOne
    Replies:
    4
    Views:
    382
    ulrich
    May 18, 2005
  5. Harvey Twyman
    Replies:
    8
    Views:
    590
    August Derleth
    Oct 25, 2003
Loading...

Share This Page