|Again Richard MacCutchan is correct so let me explain my code
name is a "pointer to a char" NOT a char
So this line
if (name && namemax > 1)
Is a shortcut of writing
if ( (name != 0) && (namemax > 1) )
Basically checks if the thing called "anything" is not zero its a shortcut
So what the line it is checking is the pointer called name is not 0 (or null which is an invalid pointer)
the space for the buffer name is greater than 1 .. why well the code specifically
makes an '\0' terminated C string so it needs a minimum buffer size of 1 to hold the '\0'
So name == 0 or namemax == 0 would bug the code so they are specifically handled.
So the line is a specific safety to make sure you could NEVER bug the routine doing
either the function will simply return 0 which is correct it read no character data.
There are a couple of other possible background bugs and program hints which was in
my hint to change the interface.
1.) The use of "stdin" could be possibly problematic in some esoteric systems .. you
don't know it's actually a device there might be no keyboard etc on the system. As such
bringing it in thru the interface pass that responsibility out to the caller code.
2.) We don't ever change name or namemax internally so placing a const in front of them
makes it clear that is the case and also allows the optimizer to really go to work. So
it's not a bug fix but a help to the compiler and anyone using the code.
So that is as robust to function input error as I can make your function.
You are left with two possible errors outside my control passing in a pointer to some
wrong place and passing in a wrong maximum buffer size. I would have to be a mind reader
to be able to deal with those but I have dealt with all the obvious possible errors.
In vino veritas
modified 4-Jan-18 21:08pm.