• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 598
  • Last Modified:

Accessing argv outside of main in UNIX

With MSFT VC++ it is possible to obtain argv info trough a global variable defined in stdlib.g (__argv).

Is there a similar way to obtain argv info outside of main in UNIX/C ?

Thank you
  • 2
  • 2
1 Solution
in unix scripts, you can reference $0 $1 $2 etc...

in C (in unix) you have to define main as:
void main(int argc; char **argv)
the compiler will do the rest and you can reference the string with argv[0], argv[1] etc...

to obtain these values outside of main, you must pass the strings as a parameter. to the function you're calling

jpintoAuthor Commented:
If your answer was a peremptory "NO" (explaining why) I would accept it.

Obviously I could pass the argv parameter trough the program structure, but this does not appears to be "clean" programming.Instead I would prefer to declare a global variable and use it when needed.

The question is :
Is that global variable already defined in some include file ?

Is there a system call that enables the capture of this information (processual info) ?

Thanks anyway.
There is no global variable that is already used. that's a ms convention. If you declare you're own global variable then you can be confident that you're not duplicating anyone elses info.

I thought about maybe using getenv() in order to get info on the process but that doesn't work.

Looks like you will need a global variable in some header, and then include it in all the files you need it

jpintoAuthor Commented:
I accept that ! Although I am sorry !!!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

We Need Your Input!

WatchGuard is currently running a beta program for our new macOS Host Sensor for our Threat Detection and Response service. We're looking for more macOS users to help provide insight and feedback to help us make the product even better. Please sign up for our beta program today!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now