[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

The following works in Linux

parag_paul@hotmail.com

12/3/2008 12:34:00 PM

#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
4 Answers

Christian Hackl

12/3/2008 1:44:00 PM

0

Hi,

First of all, you shouldn't put important information into the subject
line without repeating it in the body of your message. You claim that
"The following works in Linux", but since when is "Linux" a compiler?
Linux is an operating system, or rather a family a family of operating
systems.

parag_paul@hotmail.com ha scritto:

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

This looks like C code. Are you sure it has anything to do with C++ at
all? Anyway, an array size of 0 is not valid C++.

If by "Linux" you actually mean "GCC", try turning it into an actual C++
compiler, e.g. by adding the -pedantic and -std=c++98 options.


--
Christian Hackl

Christian Hackl

12/3/2008 1:45:00 PM

0

Hi,

First of all, you shouldn't put important information into the subject
line without repeating it in the body of your message. You claim that
"The following works in Linux", but since when is "Linux" a compiler?
Linux is an operating system, or rather a family of operating
systems.

parag_paul@hotmail.com ha scritto:

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

This looks like C code. Are you sure it has anything to do with C++ at
all? Anyway, an array size of 0 is not valid C++.

If by "Linux" you actually mean "GCC", try turning it into an actual C++
compiler, e.g. by adding the -pedantic and -std=c++98 options.


--
Christian Hackl

Maxim Yegorushkin

12/3/2008 3:09:00 PM

0

On Dec 3, 12:34 pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
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#Z...

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

James Kanze

12/3/2008 9:51:00 PM

0

On Dec 3, 1:34 pm, "parag_p...@hotmail.com" <parag_p...@hotmail.com>
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:james.kanze@gmail.com
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