Reputation: 341
I have used the below code to print the environmental variables.In that I have doubt is there a connection between the parameters of char *argv[]
and char *envp[]
in main function.
Sample Code:-
#include <stdio.h>
int main(int argc, char *argv[], char *envp[])
{
int index = 0;
while (envp[index])
printf("%s\n", envp[index++]);
}
While executing the program after removed the arguments argc and argv I get segmentation fault. Some one please explain this.!
Upvotes: 0
Views: 450
Reputation: 91
main is called int main(int argc, char** argv, char** envp).
If you remove argc, argv, it will be
int main(char* envp[])
so envp will be set to argc, and there will be segmentation fault when envp[index++]
Upvotes: 1
Reputation: 10665
It doesn't matter what the arguments are called; only their position matters.
If you removed argc
and argv
, and so you only have
int main(char *envp[])
this is illegal (since the first argument should be an integer).
What is the problem with including argc
and argv
, but not using them?
Also, I should point out that envp
is not portable. But it is accepted by the most widely used C implementations.
Upvotes: 1