Discussion:
Typedef for a structure with an array of uknown number of elements.
(too old to reply)
Vladimir Grigoriev
2008-05-08 12:15:01 UTC
Permalink
I may specify an array of undefined number of elements as a function
parameter. For example,

int main( int argc, char * argv[] );

Can I define a typedef for such array inclosed in a structure? Something as

typedef struct
{
size_t count;
Some_Type array[];
} MyTypeDef;

I have a memory extend that have such structure. However I don't know the
number of the array elements. This extend is passed from a module written on
another language.

Vladimir Grigoriev
Ed Mulroy [TeamB]
2008-05-08 13:17:08 UTC
Permalink
The function prototype for main that you show specifies a pointer to an
array of pointers. It does not specify an array of unspecified dimension.

A structure as defined has a size. Were an array of unspecified dimension
to be part of it, its size would not be defined.

What has been done in the past with such a situation is to define a
structure whose last element is of dimension 1. You can establish a typedef
for such a structure.

They then assign the address of a memory block containing the data of
interest to a variable which is the pointer to such a strucure. The array
elements can then be accessed with impunity because C and C++ provide no
array bounds checking.

. Ed
Post by Vladimir Grigoriev
I may specify an array of undefined number of elements as a function
parameter. For example,
int main( int argc, char * argv[] );
Can I define a typedef for such array inclosed in a structure? Something as
typedef struct
{
size_t count;
Some_Type array[];
} MyTypeDef;
I have a memory extend that have such structure. However I don't know the
number of the array elements. This extend is passed from a module written
on another language.
Vladimir Grigoriev
2008-05-08 14:41:44 UTC
Permalink
Post by Ed Mulroy [TeamB]
The function prototype for main that you show specifies a pointer to an
array of pointers. It does not specify an array of unspecified dimension.
Is not it an array of pointers to character, it is?!

Vladimir Grigoriev
Alan Bellingham
2008-05-08 15:11:44 UTC
Permalink
Post by Vladimir Grigoriev
Is not it an array of pointers to character, it is?!
It *may* be, but there's no guarantee. What the function receives is a
pointer to a pointer to a char. You have to go check the documentation
to see that it's actually an array of pointers to arrays of char.

The declaration may be considered part of the documentation.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Chris Uzdavinis (TeamB)
2008-05-08 13:25:50 UTC
Permalink
Post by Vladimir Grigoriev
I may specify an array of undefined number of elements as a function
parameter. For example,
int main( int argc, char * argv[] );
Well..... not really.

When you pass an array to a function, it is in appearance only. The
language requires the compiler to literally implement the function as
a pointer.

void f(char * c[10])

the REAL signature is:

void f(char * * c);

which is why this is not allowed:

void f(char * c[10]) {}
void f(char * c[9]) {} // not an overload, but a redeclaration
Post by Vladimir Grigoriev
Can I define a typedef for such array inclosed in a structure? Something as
typedef struct
{
size_t count;
Some_Type array[];
} MyTypeDef;
No.

Best you can do is

template <int Size>
struct MyTypeDef
{
size_t count;
Some_Type array[Size];
};

But that obviates any sort of runtime decision, and makes count
probably useless.
Post by Vladimir Grigoriev
I have a memory extend that have such structure. However I don't
know the number of the array elements. This extend is passed from a
module written on another language.
How about converting it into a vector?
--
Chris (TeamB);
Alan Bellingham
2008-05-08 15:29:05 UTC
Permalink
Post by Chris Uzdavinis (TeamB)
Best you can do is
template <int Size>
struct MyTypeDef
{
size_t count;
Some_Type array[Size];
};
Interestingly, BCB5 will accept

struct t
{
int x;
char c[];
};

though Comeau won't.

I've certainly seen

struct t
{
int x;
char c[1];
};

used for cases where the structure will always have at least one byte of
extra stuff in it. A similar example is in the Windows headers, for the
Metafile record format:

typedef struct tagMETARECORD {
DWORD rdSize;
WORD rdFunction;
WORD rdParm[1];
} METARECORD, *PMETARECORD;

where the actual records had a variable number of WORDs in the rdParam
array, depending on the exact code in rdFunction (and rdSize lists the
total size of the record for easier traversal).

It's instructive to compare with the EMR type in the Enhanced Metafile
Record format, where the base EMR

typedef struct tagEMR {
DWORD iType;
DWORD nSize;
} EMR, *PEMR;

is then the first member of all the individual structs such as EMRARC

typedef struct tagEMRARC {
EMR emr;
RECTL rclBox;
POINTL ptlStart;
POINTL ptlEnd;
} EMRARC, *PEMRARC;


Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Alex Bakaev [TeamB]
2008-05-08 17:09:00 UTC
Permalink
Post by Alan Bellingham
Interestingly, BCB5 will accept
MSC compatibility. There used to be tons of structures like that in
Windows headers.
Alan Bellingham
2008-05-08 17:37:29 UTC
Permalink
Post by Alex Bakaev [TeamB]
Post by Alan Bellingham
Interestingly, BCB5 will accept
MSC compatibility. There used to be tons of structures like that in
Windows headers.
It's a very Microsofty thing to do.

Alan Bellingham
--
Team Browns
ACCU Conference 2009: to be announced
Loading...