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?

Recommended Answers

All 2 Replies

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);

}
commented: Quite so. +17

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.