LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-02-2013, 09:33 AM   #1
methodtwo
Member
 
Registered: May 2007
Posts: 146

Rep: Reputation: 18
argv[1] not recognised outside of main


Hi
This is a C programming question. I have a function that gets called from main. In the definition of the function i use getaddrinfo. One of getaddrinfo's arguments is argv[1]. Because this function is defined outside of main, gcc complains that this function doesn't know what argv[1] is. I put the networking code in this function in question because sendto(this is a UDP client) doesn't know what the res structure is if i put it in main. Do i pass *argv[] as an argument to this function in question or what?
I can post the code if you want.
thank you very much for your time.

Last edited by methodtwo; 01-02-2013 at 09:35 AM.
 
Old 01-02-2013, 09:42 AM   #2
methodtwo
Member
 
Registered: May 2007
Posts: 146

Original Poster
Rep: Reputation: 18
Oh i just did:
char **arg
as a global array. Then:
arg = argv
in main. The compiler doesn't complain about this. Please just reply if you don't think the solution is right!
 
Old 01-02-2013, 06:40 PM   #3
Peverel
Member
 
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 118

Rep: Reputation: 24
It's OK. What I usually do is to pass argv as a parameter to the function that is being called.
 
Old 01-03-2013, 08:06 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,136

Rep: Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336Reputation: 1336
Quote:
Originally Posted by methodtwo View Post
Oh i just did:
char **arg
as a global array. Then:
arg = argv
in main.
It would probably be nicer to take just pass argv[1] (via global or parameter):
Code:
char *address = argv[1]
That way, if you start allowing options you only have to change the code in main() and not the networking code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
C programming linux copying argv to wchar_t argv babbab Programming 1 08-09-2012 05:32 AM
[SOLVED] why char *argv[] declaration is ok in main arg, but not in body? hppyhjh Programming 5 09-13-2010 08:36 PM
LXer: int main() vs void main() LXer Syndicated Linux News 0 01-09-2009 02:11 PM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
Help with argv pilot1 Programming 7 08-23-2003 02:20 PM


All times are GMT -5. The time now is 10:27 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration