function params' name

SGDK only sub forum

Moderator: Stef

Post Reply
KanedaFr
Administrateur
Posts: 1139
Joined: Tue Aug 29, 2006 10:56 am
Contact:

function params' name

Post by KanedaFr » Wed Nov 26, 2014 9:11 am

Hi there,

Something silly occured yesterday.
I wrote this code

Code: Select all

void getEnemyDistance(enemy *enemy, enemy *enemy2)
and when I compile, I got an error "near *"
while it works with

Code: Select all

void getEnemyDistance(enemy *e, enemy *f)
From what I understood, it seems GCC is getting crazy with all these "enemy" keywords
So, 2 questions :
- is it normal ?
- is there some official C doc which say it's wrong to do it the way I do, so I could fix it the good way ?

twosixonetwo
Very interested
Posts: 58
Joined: Tue Feb 25, 2014 3:38 pm

Post by twosixonetwo » Wed Nov 26, 2014 10:21 am

This is just a guess:
Is enemy a struct? If so I think it's possible to use it both as a type and as a variable name. Still, if its once used as a variable name, the variable name "shadows" (not sure thats the correct term) the type name in that scope. So if that's correct your function gets parsed as:
void f1(t1 *v1, v1 *v2)
Which would explain why this doesn't work.

KanedaFr
Administrateur
Posts: 1139
Joined: Tue Aug 29, 2006 10:56 am
Contact:

Post by KanedaFr » Wed Nov 26, 2014 11:16 am

I forgot to say than

Code: Select all

void anotherFunction(enemy *enemy)
works like a charm

It's onky when I use more than 1 param with the same type (yes, a struc) and one of it named like the type

really strange....

twosixonetwo
Very interested
Posts: 58
Joined: Tue Feb 25, 2014 3:38 pm

Post by twosixonetwo » Wed Nov 26, 2014 12:35 pm

That perfectly fits my guess though:
As soon as you once have used it, the label "enemy" no more refers to the type, but instead refers to the object.
So
void getEnemyDistance(enemy *enemy, enemy *enemy2)
looks the same to the compiler as
void getEnemyDistance(enemy *foo, foo *enemy2)
and since [objectname] * [objectname] is not valid syntax, you get an error.

Post Reply