473,473 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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.
Jul 28 '05 #1
4 1767
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
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

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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Berislav Lopac | last post by:
With the advent of PHP5, with its OO support and the new __autoload function, I was thinking of implementing some king of scheme to organize classes in such a way that they can be easily found by...
10
by: Adam Warner | last post by:
Hi all, With this structure that records the length of an array of pointers as its first member: struct array { ptrdiff_t length; void *ptr; };
2
by: Christopher Benson-Manica | last post by:
Is the following program conforming under C99? #include <stdio.h> typedef struct foo { int bar; int baz; } foo; foo foos={
2
by: DevarajA | last post by:
Can someone help me understand what flexible array members exactly are, how they behave and how could them be implemented by a i386? Also I didn't understand the two exceptions that the standards...
8
by: ulyses | last post by:
I'm trying to put pointer to flexible array of structures in other structure. I want to have pointer to array of pixels in screen structure. Here is mine code, but I think it isn't quite all right:...
20
by: mechanicfem | last post by:
I thought (as they're in c99) that flexible arrays were there for a number of reasons - but I also thought they'd be great for stepping into structures (say) that were aligned in memory after (say)...
2
by: tobiasoed | last post by:
Hello, I want to do something similar to this: #include <stdlib.h> struct coord { double val; double err; };
3
by: chrismcb | last post by:
In C++ I want to statically create an array that contains variable arrays inside of it. Or something similar. Essentially I want to do something like this: { {"name1", {a, b, c} }, {"name2",...
3
by: Hallvard B Furuseth | last post by:
to find the required alignment of a struct, I've used #include <stddef.h> struct Align_helper { char dummy; struct S align; }; enum { S_alignment = offsetof(struct Align_helper, align) };
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.