CompuServe Messages

main(argc,argv)

    21-Jun-93 23:11:46
Sb: #35450-main(argc,argv)
Fm: Brian Bartlett 72037,570
To: Bart Mathias 72017,1005
Bart, I don't know enough to help you with the stack question, but I think I can help you with the rest. The (argc, *argv[]) construct is quite common in C. When a program is executed on most systems, the whole argument line from the CLI is passed to the executing program. Using a fairly simple routine, it is possible to parse the argument string and figure out what the user typed so that arguments are accepted. K&R (Kernighan & Ritchie) "The C Language," Second Edition, provides an adequate example of how to parse CLI arguments in pages 114-118. As you stated, a knowledge of the C language helps immensely with programming this beast. I did wonder as to your use of (argc, **argv). The first value is the number of arguments, including the first argument, i.e. the program name, that is passed to the called program. The second argument, *argv[], is an array of strings (characters) that can be indexed into to parse the strings. While **argv is "technically" correct, the more familiar usage is *argv[] (which is exactly the same thing as **argv). What does this mean to you? A bit of discussion is called for. In C, pointers are one of the more powerful tools available to the programmer. I'm sure, at this point, that you understand "what" a pointer is (i.e. an address of a data _value_), but I don't think you understand the syntax involved. In C, when you declare a pointer, you declare it in the following syntax: long *x; where x is the address of some long integer (i.e. 32 bit integer) you have somewhere, and *x is the value stored there. (Confused? I was! * in the declaration of a variable tells the compiler that you want to store an address, * in the usage of a variable means "I want the contents referred to by this address"). It is a given that you may already have the data _value_ stored somewhere, all you are doing here is telling the compiler to provide space to store the _pointer_ (i.e. address of) that _value_. Okay, now that you have the pointer, what do you do with it? Well, you can have it point to successive values stored in the system. If you have 20 long integers stored _succesively_, you can simply bump (increment) the pointer by one to point to the next value in memory. If, for instance, you had 3 long integers stored in memory, in successive memory locations, you could do an assignment picking off each of the successive values. Look at the following code fragment: long a[]={32, 64, 128}; /* Array of three elements 32, 64, 128 */ long *x=a; /* Assign pointer x to a[0]'s location */ printf("%l",*x); /* print first element */ printf("%l, *x+1); /* print second element */ printf("%l, *x+2); /* print third element */ As you will notice, bumping the pointer automagically skips, no matter what the element size, be it short, long, or even a complex record, to the next element, as it should, since we _are_ incrementing the "address of" some structural element's pointer. One of the curious, but nice, features of C is that array names behave like pointers, i.e., the first array element (element 0) is exactly the same address as a pointer of the same name. They are one and the same. So, when you see **argv, this is the same thing as *argv[0], which means, I've passed you a pointer to the first element of an array. You can bump this variable to point to the next element in the array. **argv+1 should give you the next string in the array of strings. So, if you, the user, had typed in a command line as follows: Whatzit abc def The programmer would see, passed to him, is an array of three strings with the following values: **argv = Whatzit **argv+1 = abc **argv+2 = def What is passed to you, the programmer, is a sequence of strings in an array. What you need to do is create a loop that, using argc (the ARGument Count) as the loop counter, grabs each of the strings and parses the meaning, or non-meaning, of each of the arguments. Each element will end in a C standard null byte, i.e. \0 = 0x00. The first argument *argv[0], or **argv, is, supposedly, the program name of your program. I hope this helps. Brian