Rahul
Rahul

Reputation: 5049

What does 4th argument of main function in C point to?

We have

int main(int argc, char** argv, char** envc)

for the ordinary. but I want to know if there is any other argument main can have instead of these. And if there is any, what does it point to?

Upvotes: 3

Views: 1036

Answers (5)

Loki Astari
Loki Astari

Reputation: 264361

It is an extension to the standard:
But it is supposed to provide access to the environment:

int main(int argc, char** argv, char** envc)
{
    // It is an array of pointers to C-String
    // The array is terminated with a NULL pointer.
    // So you can loop over with it like this.
    for(int loop = 0;envc[loop] != NULL; ++loop)
    {
        fprintf(stdout, "%s\n", envc[loop]);
    }
}
  • argc: number of valid elements in argv
  • argv: an array of C-Strings for the command line arguments.
  • envc: an array of C-Strings for the environment (terminated by NULL pointer).

It is probably better to use getenv:

char * getenv ( const char * name );

http://www.cplusplus.com/reference/clibrary/cstdlib/getenv/

Upvotes: 1

Kerrek SB
Kerrek SB

Reputation: 476960

The answers differ in C and in C++:

  • In C++, main must always return int. Every implementation must accept () and (int, char**) signatures. An implementation may accept any other signature. If an accepted signature begins with int, char**, ..., those should have the usual meaning. (Also, main gets C linkage, mustn't be overloaded, mustn't be a template, and mustn't be called.)

  • In C, main may take any form. However, every implementation must accept int(void) and int(int, char**) types.

As you have noticed, one popular signature supported by certain environments, and conforming with these guidelines, is int main(int argc, char * argv[], char * env[]), in which the third argument contains a pointer to the environment. Other extensions are conceivable; check the documentation of your platform.

Upvotes: 4

user1434698
user1434698

Reputation:

I think this answers your question:

http://en.wikipedia.org/wiki/Main_function#C_and_C.2B.2B

Upvotes: 2

Linuxios
Linuxios

Reputation: 35783

Well, heres the breakdown:

  • argc -- C standard
  • argv -- C standard
  • env -- Works on most UNIX and MS Win, but not standard
  • apple -- Other information passed as forth argument by Mac OSX and Darwin

Upvotes: 8

Vaughn Cato
Vaughn Cato

Reputation: 64308

Only argc and argv are standard arguments. Anything after that depends on your system and compiler.

Upvotes: 3

Related Questions