Charan2628 Charan2628 - 3 years ago 202
C Question

Can we use integer pointer in command line arguments

main(int argc,char *argv[])
{
......;
......;
}


Here argc is an interger variable and indicates the number of parameters passed.
argv is an array of pointers to characters
My doubt is I seen an program using

main(int argc, int*argv[])
{
......;
......;
}


is it valid can we use argv as an array of pointers to integers?

Answer Source
#include <stdio.h>

int main(int argc, int*argv[])
{
int a=3;
int b=4;

printf("\n%s", (char*)argv[1]);
return 0;
}

Command Line arguments : abcd

Output : abcd

I have compiled the code above and worked perfectly. It seems that int* argv[] is permitted. I think this is compiler specific some will allow it and other will not. You have to provide appropriate casting for the argument passed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download