Re: small string question



Bill Cunningham wrote:
Wait a minute I think I might've just caught my problem. It's in the function body where I'm making the error. Not assigning name[] to anything in the body. What about this.

char passw(char name[])
{char i=name;

As cr88192 noted, i is the wrong type. It should be char* instead of char.

....
if (strcmp(i,name2)==0) {puts"ok");}

Your code doesn't show a definition for name2, but if it is a string, the comparison is OK. Note that "string.h" must be included.

else if(strcmp(i,name2)!0) {puts("unequal. Try again"); return passw();}

That should be !=0 instead of !0. Also, you don't need to make the comparison again, since it exactly the opposite of the first one.

I started to show corrections in a rewritten version, but saw a more fundamental problem. I realized that by "return passw();" you were attempting to retry the password verification. The call is not passing the required parameter and is not returning the promised character value.

Why is the function defined to return a char? If you are thinking of the password, that would be returned in the array whose address is passed. If the code only exits with a verified password, you don't need any status. You might, however, have an option for the user to cancel, in which case you might want the return a status value.

The recursion you have written is a poor way to retry a password entry. It requires extra resources for recursion and also subjects the code to crashing if repeated failures are made. An appropriate code structure is an iterative loop, as shown in the following pseudocode:
do forever {
read password
read second copy
if (match) print "match" and return
else print "reenter"
}

--
Thad
.



Relevant Pages

  • [2.4 patch][1/6] lmc_media.c: fix gcc 3.4 compilation
    ... 'lmc_trace': function body not available ... void lmcConsoleLog(char *type, unsigned char *ucData, int iLen); ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: small string question
    ... function body where I'm making the error. ... char passw ... to so blatently violate them, ... are many common, specific, and generally informally enforced conventions. ...
    (comp.lang.c)
  • Re: small string question
    ... function body where I'm making the error. ... char passw ... there are many such conventions, but going too much into specifics tends to ... to adhere to the common conventions unless there is some good reason to do ...
    (comp.lang.c)
  • Re: small string question
    ... function body where I'm making the error. ... Not assigning nameto anything ... char passw ... else if!0) {puts("unequal rtry again"); ...
    (comp.lang.c)
  • Re: Question about a solution to excercise 4-13 in K & R
    ... Don't we also increase the stack depth on a Binary Search Tree ... recursion to add a node to a Binary Search Tree. ... char tmp = *p; ...
    (comp.lang.c)