sizeof()

Discussion of issues related to writing ZBasic applications for targets other than ZX devices, i.e. generic targets.
Post Reply
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

sizeof()

Post by stevech »

shouldn't "sizeof()" return an unsigned int rather than an int?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: sizeof()

Post by dkinzer »

stevech wrote:shouldn't "sizeof()" return an unsigned int rather than an int?
Perhaps. Since it is always evaluated at compile-time, we could probably change it to a special internal "integral" type that is compatible with (e.g. can be assigned to) all integral types.
- Don Kinzer
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

can't sizeof() be evaluated at compile time if the arg is known? I use that in C when defining nested structs and unions (no forward references).

On unions...
In ZBasic, can one emulate a C union as such (I use this construct a lot)

public headerMAC16 as MACheader_t
public headerMAC16_bytes() as byte based headerMAC16.dataAddress ' like a union in C
or is there a better way to mimic a union?

were I want to access the data as an array of bytes as well as its structured type? (knowing to watch data alignment). I do this kind of thing when sending a struct of binary types as a byte stream, as in communications protocols.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

stevech wrote:In ZBasic, can one emulate a C union as such (I use this construct a lot)
Unions are supported in v4.0.0. See www.zbasic.net/doc/ZBasicRef.php?page=99. Unions were added primarily in support of importing C/C++ header files but they are useful in their own right.
- Don Kinzer
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

gracias
Post Reply