Re: [cc65] snapshot

Date view Thread view Subject view

From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2003-09-01 13:39:41


On Sun, Aug 31, 2003 at 06:17:27AM +0200, Groepaz wrote:
> 1) i think this is c++ syntax (or a gnu extension?!), i dont remember :)

Yes, the second one is C++.

> 2)
>
> ok:
>
> void vector_init(struct vec3_t* v, int x, int y, int z)
>
> not ok:
>
> void vector_init(vec3_t* v, int x, int y, int z)
>
> (with vec3_t defined as the working version in 1))

Same here. In C, structs do always have a separate namespace, so the "struct"
keyword is mandatory (if no typedef is used).

>  error: undefined symbol "FIXED_BITS"
>
> nested macro(s) is the problem here i think :)

The cc65 preprocessor is cheating, when it comes to macros calling each other.
Instead of disallowing the expansion of a macro that has already been expanded
in an upper layer, an upper limit on macro expansions is applied. The
expression you have runs into this limit. As a quick workaround, I will
increase the hardcoded number. The correct solution would be to fix macro
expansion, but this will take more time.

> void entity_to_world_collision(struct entity_t* e, int const radius);
>
> not ok: compiles but causes errors later (wrong target pointer type in
> expression like (struct entity_t*)foo=e;

Be sure that you have defined "struct entity_t" in global scope. Otherwise,
you're defining a new struct in the local function scope, and this one is
incompatible with the global one.

> void entity_to_world_collision(struct entity_t* const e, int const radius);
>
> there generally seem to be hickups with struct and const beeing used together
> in a declaretion..... btw is there a subtle difference between "const struct
> foo *bar" and "struct foo* const bar" ? i didnt even knew that the latter is
> legal c :=P

There is not only a subtle difference but a big one:

    const struct foo* bar;
        defines a pointer to a constant struct foo.

    struct foo* const bar;
        defines a constant pointer to struct foo.

So in the first case, the actual struct data is const and cannot be changed,
while in the second case just the pointer is const and cannot be changed,
while the data it points to can.

The general rule when applying const or volatile modifiers is:

    The modifier is applied to the item *left* from it. The only exception is:
    If there is nothing left of the modifier, this is equal to the modifier
    being moved one position to the right.

So the following two declarations have the same meaning, they declare a
pointer to constant char:

    const char* p;
    char const* p;

In the case above, one can change the pointer, but not the data it points to.
The next example shows a constant pointer:

    char* const p;

The pointer itself cannot be changed, but the data it points to can. If you
want to make both const, you have to use

    char const* const p;
or
    const char* const p;

This is a constant pointer to constant character data.

You may think yourself about all the nice combinations of "pointer to pointer":

    char** p;

You can have a constant pointer to pointer to char, a pointer to a constant
pointer to char, or a pointer to pointer to constant char - and of course all
combinations of it.

If you need some practice on this, you can try all combinations of const and
volatile for "struct q_t*** q" :-)

There's also a C program that takes a C declaration as input and translates it
into english - unfortunately I'm so old that I cannot remember the name. At
least my version was buggy, since it translated

        const char* p;

but not

        char const* p;

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-09-01 13:40:02 CEST