0
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?

  • 1
    Did it work when you tried it? – J...S Oct 01 '17 at 05:22
  • no I didn't i had a compiler app in my phone but it didn't take command line arguments –  Oct 01 '17 at 05:25
  • 1
    No, you can't. It is not one of the forms allowed to main – Antti Haapala -- Слава Україні Oct 01 '17 at 05:47
  • 1
    The code you vaguely remember probably made some daring assumptions, which might have worked on the specific platform it was written for. Otherwise, see comment by AnttiHaapala. – Yunnosch Oct 01 '17 at 05:50
  • See [What should `main()` return in C and C++?](http://stackoverflow.com/questions/204476/) for details of allowed forms. The short answer is "no". The o/s will pass an array of strings to `main()` unless you have special startup code that does differently. If you knew how to do that, then you wouldn't need to ask this question. (Also note that neither C99 nor C11 allows `main(int argc, char **argv)`; you must specify the return type explicitly in any vaguely modern variant of C. Only the archaic C90 standard allowed the implicit `int`. – Jonathan Leffler Oct 01 '17 at 06:07
  • [Related](https://stackoverflow.com/q/11064538/335858) – Sergey Kalinichenko Oct 01 '17 at 09:11

1 Answers1

-5
#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 in compilers I tried. It seems that int* argv[] is permitted in some compilers. I think this is compiler specific some will allow it and other will not. You have to provide appropriate casting for the argument passed.

NB : I am not saying it works with every compilers and every environment. :)

Sreeram TP
  • 11,346
  • 7
  • 54
  • 108
  • What? Testing with one particular compiler and you deduce that this valid C? I'd suggest that you put the warning level up (`-Wall` or similar). In any case, `int*` is **wrong** unless you have a special environment that explicitly allows *and* documents this. – Jens Gustedt Oct 01 '17 at 09:07
  • Thanks for the information. I tried about 5 online compilers, cc, gcc, mingw in my machine. And I have stated it is compiler specific in my answer. – Sreeram TP Oct 01 '17 at 09:55
  • 1
    `main.c:3:5: warning: second argument of ‘main’ should be ‘char **’ [-Wmain] int main(int argc, int*argv[])`. You don't know for sure what happened under the hood that makes the output acceptable. – CroCo Oct 01 '17 at 09:57