I thought that the first program here could be written like this:
Code:#include <stdio.h> int main(int argc, char *argv[] ) { printf("I am the child\n"); while(--argc) printf ("Arg %d %s\n", argc-1, *++argv); return 0; }
I thought that the first program here could be written like this:
Code:#include <stdio.h> int main(int argc, char *argv[] ) { printf("I am the child\n"); while(--argc) printf ("Arg %d %s\n", argc-1, *++argv); return 0; }
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
Actually, both methods have a potential flaw. They never check for argc being zero. There is nothing which dictates that the program's name ever must be stored in argv[ 0 ]. That is to say, nothing says is will be there for sure. To quote a reference:
There's no reason you have to have the program name available. As such, argc would be zero. Decrementing it would underflow the variable, and would crash both programs.C: A Reference Manual, 5th Edition
Page 303, 9.9 THE MAIN PROGRAM, pharagraph 3.
...
The parameter argv is a vector of pointers to
strings representing the program arguments. The first string, argv[0], is the name of the program; if the name is not available, argv[0][0] must be '\0'.
Quzah.
Hope is the first step on the road to disappointment.
You have the wrong quote. argv[argc] must be NULL. Your quote is stating that if the name is not available then argv[0] must be an empty string, which is not NULL. What you want to quote is:
The standard then goes to describe situations where argc > 0, which is what the quote you gave is part of.The value of argc shall be nonnegative.
argv[argc] shall be a null pointer.
No, I have the quote right. If argc is zero, and you decrement it, you'll end up trying to access '++argv' some thousands, or millions, or billions of incrementations past where you're supposed to be accessing it due to variable underflow.That's the quote from the book. I don't have a copy of the standard to compare it to. But the point wasn't if it's null or not. The point was you'll increment your variable millions of times past where you should be, simply because argc is zero before you try and use it.Your quote is stating that if the name is not available then argv[0] must be an empty string, which is not NULL.
Quzah.
Hope is the first step on the road to disappointment.
If argv[0] can never be NULL (it must contain the program name or an empty string), doesn't that mean that argc must always be at least 1?
If you understand what you're doing, you're not learning anything.
how could you not know the program's name (i.e. argv[0] being NULL)?
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
and how do you find such implementation? wouldn't it be logical to use a "normal" implem.?
From that link Dave posted:
Posted by Quzah:- argv[argc] shall be a null pointer.
Hence, argc must always be at least 1. Either that or someone is wrong (Quzah or the information from that link.) Or maybe something changed between standards?The first string, argv[0], is the name of the program; if the name is not available, argv[0][0] must be '\0'.
If you understand what you're doing, you're not learning anything.
But the standard says that argv[0] will be a pointer to a string only if argc is greater than 0. So if argc is 0 then argv[0] would be NULL, and the other requirements that are dependant on argc being positive do not apply.If argv[0] can never be NULL (it must contain the program name or an empty string)
C99If they [argc and argv] are declared, the parameters to the main function shall obey the following
constraints:
— The value of argc shall be nonnegative.
— argv[argc] shall be a null pointer.
— If the value of argc is greater than zero, the array members argv[0] through
argv[argc-1] inclusive shall contain pointers to strings, which are given
implementation-defined values by the host environment prior to program startup. The
intent is to supply to the program information determined prior to program startup
from elsewhere in the hosted environment. If the host environment is not capable of
supplying strings with letters in both uppercase and lowercase, the implementation
shall ensure that the strings are received in lowercase.
— If the value of argc is greater than zero, the string pointed to by argv[0]
represents the program name; argv[0][0] shall be the null character if the
program name is not available from the host environment. If the value of argc is
greater than one, the strings pointed to by argv[1] through argv[argc-1]
represent the program parameters.
— The parameters argc and argv and the strings pointed to by the argv array shall
be modifiable by the program, and retain their last-stored values between program
startup and program termination.
Last edited by the pooper; 08-08-2005 at 11:10 AM.
right, but still, how can the program name not be available from the environment. you call a program by it's name afterall, don't you?
I can't provide a practical example either. But the point is that it is allowed to happen. Programming would be so much easier if we allowed ourselves to ignore errors that were uncommon. But robust programming means that we must consider all the possibilities.
Thanks pooper. That's what I was wondering. I don't have a copy of the standard.
If you understand what you're doing, you're not learning anything.