| 1 | = Using char array arguments = |
---|
| 2 | |
---|
| 3 | ''Part of'' ArraysCategory |
---|
| 4 | |
---|
| 5 | == Description == |
---|
| 6 | |
---|
| 7 | A basic way to use the 'args' variable in the 'main' function. |
---|
| 8 | |
---|
| 9 | == Example == |
---|
| 10 | |
---|
| 11 | {{{ |
---|
| 12 | #!d |
---|
| 13 | /* |
---|
| 14 | to use, type: |
---|
| 15 | [program name] -p [string to print] |
---|
| 16 | |
---|
| 17 | This could be more complicated, but this'll give you an idea on |
---|
| 18 | how to use arguments in your program. |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | int main(char[][] args) |
---|
| 22 | { |
---|
| 23 | // check to make sure that the number of args is correct |
---|
| 24 | if(args.length < 2) // the first argument is the program name... |
---|
| 25 | { |
---|
| 26 | printf("No arguments given.\n"); |
---|
| 27 | return 0; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | for(int i = 0; i < args.length;i++) |
---|
| 31 | { |
---|
| 32 | if(args[i][0 .. 2] == "-p") |
---|
| 33 | { |
---|
| 34 | for(int ii = i+1; ii < args.length;ii++) |
---|
| 35 | { |
---|
| 36 | printf("%.*s ", args[ii]); |
---|
| 37 | } |
---|
| 38 | printf("\n"); |
---|
| 39 | } |
---|
| 40 | } |
---|
| 41 | return 0; |
---|
| 42 | } |
---|
| 43 | }}} |
---|
| 44 | |
---|
| 45 | == Source == |
---|
| 46 | |
---|
| 47 | || Link || http://www.dsource.org/tutorials/index.php?show_example=100 || |
---|
| 48 | || Posted by || Anonymous (Jun 23, 2004 5:43 pm) || |
---|
| 49 | || Edited by || jcc7 (Sat Oct 30, 2004 3:25 pm) || |
---|
| 50 | |
---|
| 51 | |