The program compiles fine but windows keeps giving me errors when I run the program:
Code:while (t<=argc){ if (argv[t][0]=='-'){ printf("Found option"); } t++; }
The program compiles fine but windows keeps giving me errors when I run the program:
Code:while (t<=argc){ if (argv[t][0]=='-'){ printf("Found option"); } t++; }
Last edited by angelfly; 04-28-2002 at 02:18 PM.
You've exceeded the array bounds....
try
Code:while (t<argc){
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
consider:
C:\>prog A: -s -t
executes the job(prog) to the file or device A: with that options -s & -t.
for each [argc] is the number of arguments passed to prog; that are 3 (A:, -s, -t);
argv[argc] points to the first element of the argument. if argc is NULL the program name is not available,
so<?> :
consider t = 1,
argv[t] will point to the first elemet of "A:" string that's 'A'.
so if t increases t = 2, points the first element of "-s" that's '-'
use <string.h> functions to identify which letter was used after the dash('-') to apply the option
Ünicode¬>world = 10.0£
thanks it works now.I used <= because I thought if I used < and for example there were 5 arg it would stop at 4
Thats the funny thing with array indexes ! If there are 5 elements you access them using ary[0] thru to ary[4] (0 to 4 makes the 5 elements).
So, the following defines and populates an array of 5 ints, with the values 10 - 14:
Also, the argc/argv combo people use with main includes the program name as the first element, then the command line args in the remainder. So if you're looking for switches (-t -i etc), you'd best start with argv[1], not argv[0].Code:#define MAX_ELEMENTS 5 int ary[MAX_ELEMENTS]; int i; for (i = 0; i < MAX_ELEMENTS; i++) { ary[i] = i +10; }
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]