The following works in Linux

Discussion in 'C++' started by parag_paul@hotmail.com, Dec 3, 2008.

  1. Guest

    #include <stdio.h>

    struct _table_model_entry {
    struct _table_model_entry *next;
    int line_nr;
    int nr_fields;
    char *fields[0]; /* User defined */
    };


    int main(){
    char * a,*b,*c,*d;
    struct _table_model_entry tb;
    tb.fields[0] = a;
    tb.fields[1] = b;


    }



    But I dont get the use of an array size of 0 for member fields in the
    struct _table_model_entry .
    This was not compilable in AIX,
    Is this a hack
     
    , Dec 3, 2008
    #1
    1. Advertising

  2. On Dec 3, 12:34 pm, "" <>
    wrote:
    > #include <stdio.h>
    >
    > struct _table_model_entry {
    >   struct _table_model_entry *next;
    >   int line_nr;
    >   int nr_fields;
    >   char *fields[0];      /* User defined */
    > };
    >
    > int main(){
    > char * a,*b,*c,*d;
    > struct  _table_model_entry tb;
    > tb.fields[0] = a;
    > tb.fields[1] = b;
    > }
    >
    > But I dont get the use of an array size of 0 for member fields in the
    > struct  _table_model_entry .


    char *fields[0] is a gcc extension similar to C99 flexible array
    member. You can find more information here:
    http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Zero-Length.html#Zero-Length

    The storage for that member must be allocated manually. It is normally
    used with dynamic allocation:

    #include <stdlib.h>

    struct _table_model_entry {
    struct _table_model_entry *next;
    int line_nr;
    int nr_fields;
    char *fields[0]; /* User defined */
    };

    typedef struct _table_model_entry tme;

    tme* tme_alloc(int fields_count)
    {
    return (tme*)malloc(
    sizeof(tme)
    /* space for tme::fields member*/
    + sizeof(char*) * fields_count
    );
    }

    int main()
    {
    tme* p = tme_alloc(2);
    p->fields[0] = NULL;
    p->fields[1] = NULL;
    }

    --
    Max
     
    Maxim Yegorushkin, Dec 3, 2008
    #2
    1. Advertising

  3. James Kanze Guest

    On Dec 3, 1:34 pm, "" <>
    wrote:
    > #include <stdio.h>


    > struct _table_model_entry {
    >   struct _table_model_entry *next;
    >   int line_nr;
    >   int nr_fields;
    >   char *fields[0];      /* User defined */


    Illegal. An array member cannot have a dimension of 0.

    > };


    > int main(){
    > char * a,*b,*c,*d;
    > struct  _table_model_entry tb;
    > tb.fields[0] = a;
    > tb.fields[1] = b;
    > }


    > But I dont get the use of an array size of 0 for member fields
    > in the struct  _table_model_entry .
    > This was not compilable in AIX,
    > Is this a hack


    It's not compilable. I get:
    array0.cc:11: error: ISO C++ forbids zero-size array ‘fields’
    from g++ under Linux.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Dec 3, 2008
    #3
    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. Replies:
    7
    Views:
    567
    Michael Kurz
    Oct 9, 2004
  2. F. GEIGER
    Replies:
    3
    Views:
    792
    F. GEIGER
    Aug 6, 2004
  3. aditya

    how the following printf statement works

    aditya, Oct 29, 2004, in forum: C Programming
    Replies:
    9
    Views:
    474
    Joona I Palaste
    Oct 29, 2004
  4. maadhuu

    want to know hoe the following works .

    maadhuu, Aug 13, 2005, in forum: C Programming
    Replies:
    4
    Views:
    421
    Michael Mair
    Aug 13, 2005
  5. Ravi

    how the following code works?

    Ravi, Jun 16, 2007, in forum: C Programming
    Replies:
    16
    Views:
    593
    Army1987
    Jun 17, 2007
Loading...

Share This Page