By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,130 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,130 IT Pros & Developers. It's quick & easy.

Flexible array, a la C99

P: n/a
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.
Jul 28 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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} };

But of course!

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

:-)

V
Jul 29 '05 #2

P: n/a
Victor Bazarov <v.********@comacast.net> wrote:
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.
Jul 29 '05 #3

P: n/a

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 <int NUM_BARS>
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-

Jul 29 '05 #4

P: n/a
Shezan Baig wrote:
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 <int NUM_BARS>
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
Jul 30 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.