Page 1 of 1

function params' name

Posted: Wed Nov 26, 2014 9:11 am
by KanedaFr
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 ?

Posted: Wed Nov 26, 2014 10:21 am
by twosixonetwo
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.

Posted: Wed Nov 26, 2014 11:16 am
by KanedaFr
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....

Posted: Wed Nov 26, 2014 12:35 pm
by twosixonetwo
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.