Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-02-2013, 10:33 AM   #1
Registered: May 2007
Posts: 146

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

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 10:35 AM.
Old 01-02-2013, 10:42 AM   #2
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, 07:40 PM   #3
Registered: May 2009
Location: Chelmsford, England
Distribution: OpenSuse 12.2 and 13.2, Leap 4.2
Posts: 120

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, 09:06 AM   #4
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,251

Rep: Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420Reputation: 1420
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):
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.


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

All times are GMT -5. The time now is 12:35 PM.

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