C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Flexible array, a la C99

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Christopher Benson-Manica
Guest





PostPosted: Thu Jul 28, 2005 11:19 pm    Post subject: Flexible array, a la C99 Reply with quote



The following program is, to the best of my knowledge, a conforming
C99 program:

#include <stdio.h>

typedef struct foo {
int whocares;
int bar[];
} foo;

int main() {
return( 0 );
}

The declaration of the zero-size array "bar" is not legal C++. Is
there a C++ construct that will do something similar, i.e., allow
something like the following declaration?

foo myFoo={ 1, {1,2,3} };

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Back to top
Victor Bazarov
Guest





PostPosted: Fri Jul 29, 2005 12:34 am    Post subject: Re: Flexible array, a la C99 Reply with quote



Christopher Benson-Manica wrote:
Quote:
The following program is, to the best of my knowledge, a conforming
C99 program:

#include
typedef struct foo {
int whocares;
int bar[];
} foo;

int main() {
return( 0 );
}

The declaration of the zero-size array "bar" is not legal C++. Is
there a C++ construct that will do something similar, i.e., allow
something like the following declaration?

foo myFoo={ 1, {1,2,3} };


But of course!

struct foo {
int a;
int b[UINT_MAX];
};

:-)

V



Back to top
Christopher Benson-Manica
Guest





PostPosted: Fri Jul 29, 2005 12:44 pm    Post subject: Re: Flexible array, a la C99 Reply with quote



Victor Bazarov <v.Abazarov (AT) comacast (DOT) net> wrote:

Quote:
struct foo {
int a;
int b[UINT_MAX];
};

:-)

I can think of ULONG_MAX reasons why I don't think I'm going to use
that, but the margin is too small to contain them :-)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.

Back to top
Shezan Baig
Guest





PostPosted: Fri Jul 29, 2005 1:11 pm    Post subject: Re: Flexible array, a la C99 Reply with quote


Christopher Benson-Manica wrote:
Quote:
The following program is, to the best of my knowledge, a conforming
C99 program:

#include <stdio.h

typedef struct foo {
int whocares;
int bar[];
} foo;

int main() {
return( 0 );
}

The declaration of the zero-size array "bar" is not legal C++. Is
there a C++ construct that will do something similar, i.e., allow
something like the following declaration?

foo myFoo={ 1, {1,2,3} };



The best I can think of is:

template struct foo {
int whocares;
int bar[NUM_BARS];
};


But this requires that you specify the size explicitly when you
construct the object:

foo<3> myFoo={ 1, {1,2,3} };


Hope this helps,
-shez-


Back to top
Victor Bazarov
Guest





PostPosted: Sat Jul 30, 2005 2:25 am    Post subject: Re: Flexible array, a la C99 Reply with quote

Shezan Baig wrote:
Quote:
Christopher Benson-Manica wrote:
The following program is, to the best of my knowledge, a conforming
C99 program:

#include <stdio.h

typedef struct foo {
int whocares;
int bar[];
} foo;

int main() {
return( 0 );
}

The declaration of the zero-size array "bar" is not legal C++. Is
there a C++ construct that will do something similar, i.e., allow
something like the following declaration?

foo myFoo={ 1, {1,2,3} };



The best I can think of is:

template struct foo {
int whocares;
int bar[NUM_BARS];
};


But this requires that you specify the size explicitly when you
construct the object:

foo<3> myFoo={ 1, {1,2,3} };

The problem might be that different 'foo' instantiated with different
values are all different types. It's not that bad. You may specify
the number that is greater than the number of the initialisers in the
array.

V



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.