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

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-02-01 21:55:06


On Sat, Feb 01, 2003 at 04:05:45PM +0100, Christian Krüger wrote:
> In your case (with dynamic memory) 'sizeof' delivers also an 'untrue'
> value, taking your array allways with one byte into account...

There's a difference between the two: I'm explicitly cheating around the type
system of the compiler, while in your case, you expect the compiler to support
what you're doing.

> >Another problem is, that one can initialize single struct instances, but
> >not
> >arrays of structs:
>
> Won't work with dynamic allocation either...

Works easily:
                                                        
        /* Allocate space for two structures */
        struct foo* f = malloc (sizeof (foo) * 2 +
                                strlen (name1) + 
                                strlen (name2));

> It's not only convenience. As I wrote above, I won't use dynamic
> memory, so I helped myself with ugly casts and initialize the data
> in ca65 and let cc65 point on it, telling that this is the structure.
>
> The way of defining an unsized array seems much more 'portable' than
> relying upon the allocation principle of static data by a
> specific compilier:
>
> --8<----
>
> struct foo = {10,20,'T'};
> char addtionalData[] = "his is the name";
>
> --8<----
>
> ...hoping that the compiler will put the data exactly in this
> sequence into memory.

There's no need for assembler modules or questionable C code:

        struct foo {
            char x, y;
            char name[1];
        };

        static struct {
            char x, y;
            char name[4];
        } bar = {
            0, 0, "bar"
        };

        void func (struct foo* f)
        {
            ...
        }

        func ((struct foo*) &bar);

> Maybe you know a solution of defining static data of various length
> which is accessible by a structure, but I don't know how to handle
> this... (any solution would be better than none..)

See above. You can also use void variables. See the manual for more
information. 

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz_at_musoftware.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-02-01 21:58:07 CET