The following works in Linux

P

parag_paul

#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
 
M

Maxim Yegorushkin

#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;
}
 
J

James Kanze

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top