www.digitalmars.com         C & C++   DMDScript  

D - arguments and return value of main function

reply Robert <Robert_member pathlink.com> writes:
Following codes can be compiled and executed!


void main(int a, int b, int c, int d) {
printf("%08X\n", a);
printf("%08X\n", b);
printf("%08X\n", c);
printf("%08X\n", d);
}

$ ./main blah blah blah
00000004  <- argv.length
0012FF18  <- &argv[0]
0022E174
00000001


class Main {
public static int main(int argc, char[]* argv) {
for(int i = 0; i < argc; ++i) {
printf("%.*s\n", argv[i]);
}
return 0;
}
}

$ ./main hoge hoge
/main
hoge
hoge


struct Main {
private /* static */ double main(char[]* argv) {
int argc = (cast(int*)&argv)[-1];
for(int i = 0; i < argc; ++i) {
printf("%.*s\n", argv[i]);
}
return 2.3;
}
}

$ ./main foo bar
/main
foo
bar


Pls check main's format, D compiler!


"int main(int argc, char* argv[])" is a standard prototype of C/C++'s main
function.
But, the `argv' is an array object of pointers in D.
So, this prototype goes wrong in D
("int main(int argc, char[]* argv)" works because `argv' is a pointer of array
objects.)
I think that it's confusable for C/C++ programmers
(In fact, I saw one who suffers from it.)

Robert (Japanese)
Dec 08 2003
parent "Walter" <walter digitalmars.com> writes:
Hmm. I think you're right!

"Robert" <Robert_member pathlink.com> wrote in message
news:br21cq$1isd$1 digitaldaemon.com...
 Following codes can be compiled and executed!


 void main(int a, int b, int c, int d) {
 printf("%08X\n", a);
 printf("%08X\n", b);
 printf("%08X\n", c);
 printf("%08X\n", d);
 }

 $ ./main blah blah blah
 00000004  <- argv.length
 0012FF18  <- &argv[0]
 0022E174
 00000001


 class Main {
 public static int main(int argc, char[]* argv) {
 for(int i = 0; i < argc; ++i) {
 printf("%.*s\n", argv[i]);
 }
 return 0;
 }
 }

 $ ./main hoge hoge
 /main
 hoge
 hoge


 struct Main {
 private /* static */ double main(char[]* argv) {
 int argc = (cast(int*)&argv)[-1];
 for(int i = 0; i < argc; ++i) {
 printf("%.*s\n", argv[i]);
 }
 return 2.3;
 }
 }

 $ ./main foo bar
 /main
 foo
 bar


 Pls check main's format, D compiler!


 "int main(int argc, char* argv[])" is a standard prototype of C/C++'s main
 function.
 But, the `argv' is an array object of pointers in D.
 So, this prototype goes wrong in D
 ("int main(int argc, char[]* argv)" works because `argv' is a pointer of

 objects.)
 I think that it's confusable for C/C++ programmers
 (In fact, I saw one who suffers from it.)

 Robert (Japanese)

Dec 08 2003