why won't the following compile?
Hi. what am I doing wrong here. >
#include <stdio.h> int copy(int a); int main(void) { int num = 5; int c = 0; c = copy(num); printf("num = %d\n", c); return 0; } int copy(int a) { int a = num; return a; } gcc gives me the following errors but since I am fairly new I don't understand them copy.c: In function `copy': copy.c:19: warning: declaration of `a' shadows a parameter copy.c:19: `num' undeclared (first use in this function) copy.c:19: (Each undeclared identifier is reported only once copy.c:19: for each function it appears in.) it says 'num' is undeclared but I decalred it in 'main' ... thanks :) |
it's a scope issue, copy() can't see your num variable, num is local to main() move num outside of main to make it global or add int num inside of copy().
|
thanks :) havent got to scope yet but I think I know what you mean
|
I think you are redfining "int a" in copy. Scope issue is also involved. So the local int shadows the "int a" defined in copy(int a). Try this.
int copy(int a) { return a; } --Sarin |
int copy(int num)
{ int a; return a=num; } I think thats what you are trying to do! :) |
All times are GMT -5. The time now is 07:22 PM. |