LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   why won't the following compile? (https://www.linuxquestions.org/questions/programming-9/why-wont-the-following-compile-29974/)

purpleburple 09-09-2002 12:39 PM

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 :)

rshaw 09-09-2002 01:23 PM

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().

purpleburple 09-10-2002 12:18 AM

thanks :) havent got to scope yet but I think I know what you mean

sarin 09-10-2002 03:33 AM

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

The_Nerd 09-10-2002 07:12 PM

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.