LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2002, 12:39 PM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
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
 
Old 09-09-2002, 01:23 PM   #2
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,692

Rep: Reputation: 45
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().
 
Old 09-10-2002, 12:18 AM   #3
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks havent got to scope yet but I think I know what you mean
 
Old 09-10-2002, 03:33 AM   #4
sarin
Member
 
Registered: May 2001
Location: India, Kerala, Thrissur
Distribution: FC 7-10
Posts: 354
Blog Entries: 2

Rep: Reputation: 34
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
 
Old 09-10-2002, 07:12 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
int copy(int num)
{
int a;
return a=num;
}

I think thats what you are trying to do!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I compile? icecream33 Programming 3 12-20-2017 11:49 AM
compile 2.6.13 jupiter3437 Linux - Software 3 09-09-2005 04:09 PM
ntop compile/post-compile problem? tjb Linux - Software 3 12-28-2004 04:22 PM
Cannot compile c davidtsl Programming 8 12-13-2004 01:36 AM
Compile help Twiggy794 Linux - Software 1 12-03-2003 01:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration