0

Hi guys. I cannot figure out how to do next thing.
for example i have a little progie like this:

int myfunction(int argc, char *argv[]){
int i;
  	for(i=0;i<argc;i++)
    	unlink(argv[i]);
}
int main(int argc, char *argv[]){
myfunction(argc,argv);
}

works fine, but i'm trying to do that:

int myfunction(int argc, char *argv[]){
int i;
  	for(i=0;i<argc;i++)
    	unlink(argv[i]);
}
int main(){
int argc;
char **argv;
myfunction(argc,argv);
}

and it's not working. and i got no idea how to get rid from int main declaration of argc and argv. Can anyone help me plz?

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by LouPascalou
1
int myfunction(int argc, char *argv[]){
int i;
  	for(i=0;i<argc;i++)
    	unlink(argv[i]);
}
int main(){

int argc; // argc is not initialized to any value, what might be its value here ??
char **argv; // argv is not initialized to any value, what might be its value here ??

// Now you call myfunction() with random data, expect most anything ...
myfunction(argc,argv);

}
Votes + Comments
Quite so.
0

I do think that your program really should look like:

int myfunction(int argc, char *argv[]) {
int i;
  	for(i=0;i<argc;i++)
    	unlink(argv[i]);
}
int main(int argc, char **argv) {

myfunction(argc,argv);
}

That means that the arguments to your program are passed directly to your function.
The drawback with starting your loop with i=0, you will unkink your very own executale name, which might not be what you want to acheive.

Hope this helps,
Pascal.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.