LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   argv[1] not recognised outside of main (https://www.linuxquestions.org/questions/linux-newbie-8/argv%5B1%5D-not-recognised-outside-of-main-4175443814/)

methodtwo 01-02-2013 09:33 AM

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.

methodtwo 01-02-2013 09:42 AM

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!

Peverel 01-02-2013 06:40 PM

It's OK. What I usually do is to pass argv as a parameter to the function that is being called.

ntubski 01-03-2013 08:06 AM

Quote:

Originally Posted by methodtwo (Post 4861296)
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.


All times are GMT -5. The time now is 12:03 AM.