 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christopher Benson-Manica Guest
|
Posted: Thu Jul 28, 2005 11:19 pm Post subject: Flexible array, a la C99 |
|
|
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
|
Posted: Fri Jul 29, 2005 12:34 am Post subject: Re: Flexible array, a la C99 |
|
|
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
|
Posted: Fri Jul 29, 2005 12:44 pm Post subject: Re: Flexible array, a la C99 |
|
|
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
|
Posted: Fri Jul 29, 2005 1:11 pm Post subject: Re: Flexible array, a la C99 |
|
|
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
|
Posted: Sat Jul 30, 2005 2:25 am Post subject: Re: Flexible array, a la C99 |
|
|
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 |
|
 |
|
|
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
|
|