Re: [cc65] Another wish: Unsized Arrays in Structures

Date view Thread view Subject view

From: Tim Schürmann (tischuer_at_web.de)
Date: 2003-01-23 10:17:31


Hi!

Spiro Trikaliotis schrieb:

> Groepaz: There is a difference. In your code, TestStruct contains
> a pointer to some area. This is not true with Christian's wanted
> solution.
>
> Look at this:
>
> TestStruct *a = malloc(sizeof(TestStruct)+sizeof(unsigned char)*100);

> With this, you get a TestStruct which contains 100 elements (0..99)
> [in case the 0 does not work, and David's suggestion has to be applied,
> it is 101, ie., 0..100].


Sorry, but i don't understand your argumentation.
What you got there is, that TestStruct points to a beginning of the
allocated memory of 1 TestStruct (why do you "malloc" the TestStruct?
TestStruct already exist) plus 100 Elements of unsigned char. So, does data
point automaticly to the right allocated "char-Memory"? If this is true,
the same line should work with Groepaz solution.

My C-Standard book (german version of Kernighan and Richie) says, that

char a[];
and
char *a;

are the same. Arrays acts like pointers (btw: are arrays realised as
pointers?). That's why you could write a[i] or *(a+i) and if pa is a
pointer and a the array you can do
pa=a
and
pa++
to get the next element.

So, it don't matter, if you choose the solution

struct {
...
unsigned char data[0];
}

or

struct{
...
unsigned char *data;
}

Bye,

Tim Schürmann
tischuer_at_web.de

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo_at_musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.


Date view Thread view Subject view

This archive was generated by hypermail 2.1.3 : 2003-01-23 10:16:27 CET