Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > function

Reply
Thread Tools

function

 
 
Walter Roberson
Guest
Posts: n/a
 
      03-01-2008
In article <915yj.1231$6R.955@trnddc04>,
Bill Cunningham <> wrote:
> Walter does an if statement between () have to be an arithmetic
>statement?


The part in the () in an 'if' statement must be an expression.
Every expression has a value (by definition.) The value that
results from the expression will be compared to 0, and the
first statement will be chosen if it is -not- 0 and the 'else'
statement (if any) will be chosen if the expression -is- 0.

If you code the expression with one of the logical operators,
such as if (x > 3) then it is fairly obvious what the
logical result of the expression is -- it will be either true or false.
The logical operators all return 1 to mean true and 0 to mean false.

If you code the expression with an arithmetic value but no logical
operator, then remember the implicit comparison to 0. For example
if you code if (x) then it is the same as if (x != 0).
This is the case whether x is some kind of integer or some kind
of floating point value -- it is either 0 or it isn't.

The above two paragraphs might sound like two different rules, but
they are really the same rule: does the result of the expression
equal 0 or does it not? The fact that a logical operator
happens to produce 1 for true does not matter to C: what is important
to C's 'if' statements is that the result is not 0.

What is tested in the 'if' statement can also be a pointer of
some kind. This involves a special case: when x is a pointer,
if (x) is considered true if the pointer is not NULL, and
considered false if the pointer -is- NULL. In many systems,
NULL -is- 0 internally, but that is not true in all implementations;
whatever internal value or values that the implementation uses
to represent NULL, the implementation is responsible for testing
against those internal values for the if (x) case where x is a pointer,
which is the same situation as if if (x != NULL) had been coded.


>I am thinking I am trying to test if the value of the char type
>pass is "ded" or not. A string. So I am thinking strcmp() but if a string is
>"ded I want to branch to success. If the value of pass isn't "ded" I want to
>branch to something else. A failure.


strcmp(pass, "ded") will return an arithmetic value that will be
non-zero if the strings are not the same, and will be 0 if the
strings -are- the same. Because 0 is returned if they -are- the
same, the code if (strcmp(pass,"ded") == 0) will end up
comparing 0 to 0 if the strings -are- the same, and since 0 == 0
the test would be true and the first statement after the 'if' would
be executed. Therefore your originally posted code

if (strcmp(pass,pass2) == 0)

was exactly the right kind of test to make to check to see if
the two strings were the same. The original code was marred by
the strcpy() that you had above the test, which had the effect
of forcing the two strings to be the same, so because of the
strcpy(), the two would always compare equal in strcmp(), so
0 would be returned by strcmp, that 0 would be tested with == 0
because you coded that, and that would be true, so only the first
statement after the 'if' could be reached. If you did not have
the strcpy() in there, that problem would not have been present --
but you would have still had the problem that you were fgets'ing
into your initialized variable rather than into your other variable.
--
"Pray do not take the pains / To set me right. /
In vain my faults ye quote; / I wrote as others wrote /
On Sunium's hight." -- Walter Savage Landor
 
Reply With Quote
 
 
 
 
Ulrich Eckhardt
Guest
Posts: n/a
 
      03-01-2008
Bill Cunningham wrote:
> "Ian Collins" <ian-> wrote in message
> news:...
>> As that you best you can do? I don't think anyone is going to bite this
>> time.

>
> Guess so; maybe I can figure it out myself. I hope this doesn't bait
> trolls but I'm sure it's a if else question. Only talk C please.


Okay, then C the FAQ, please.

<SCNR>

Uli

 
Reply With Quote
 
 
 
 
Robbie Hatley
Guest
Posts: n/a
 
      03-01-2008

"Bill Cunningham" wrote:

> I have been having fits with this function. Can anyone help me?


What is your program supposed to do? You don't specify.

> I think I need to use if and else if but I have rewritten
> it and got errors.


What errors do you get? You don't specify.

> I want a conditional in this program a choice between 2
> options and only one works.


Sort of like a password, eh?

> I don't think I need the strcpy either.


Then why not take it out and see what happens?

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> main(){
> char pass[10]="ded", pass2[10];


Make your arrays at least 1 larger than the strings going into
them, to allow for the NUL terminators:

char pass[11]="ded", pass2[11];

> printf("Authorization ");
> fflush(stdout);
> fgets(pass,10,stdin);


You're over-writing your "reference" password. You don't
want to do that. That last line should actually be:

fgets(pass2,10,stdin);

> strcpy(pass2,pass);


Why on earth would you want to do that? You just destroyed
your "conditional" by making *ALL* passwords pass! That's
like having a computer with password regex ".*" (any character
string you please). So just get rid of that strcpy().

> if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
> else
> printf("error"); exit(EXIT_FAILURE);}


It's better to format your code for readibility:

if (strcmp(pass,pass2)==0)
{
printf("success"); exit(0);
}
else
{
printf("error"); exit(EXIT_FAILURE);
}

There may be other errors there that I'm not seeing.
I don't have time to do your debugging for you.
I'll leave that as an exercise for the student.
But I think you'll find my tips above helpful.

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-01-2008
"Bill Cunningham" <> writes:

> [snip]
>
>> If you have only been able to trigger one of the cases, then
>> it could be that the code above the 'if' is wrong, or it could
>> be that the expression to be evaluated is wrong. I can't say
>> which at the moment as we don't know your current code.

>
> Ok thanks I will work on it more and come back if I still can't get it
> right.


If your current version is similar to the original, then the problem
you have is that fgets retains the terminating newline (the character
that marks the end of a line of input). You need either to remove
this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.

--
Ben.
 
Reply With Quote
 
Karthigan Srinivasan
Guest
Posts: n/a
 
      03-01-2008
This should work.

Best Regards,
Karthigan.

************************************************** **********
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(){

char pass[10]="ded", pass2[10];

printf("Authorization ");
fflush(stdout);

gets(pass2);

if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}

else {printf("error"); exit(EXIT_FAILURE);}

return 0;
}

************************************************** ***********


On Fri, 29 Feb 2008 21:20:43 -0600, Bill Cunningham <>
wrote:

> I have been having fits with this function.
> Can anyone help me I think I need to use if and else if but I have
> rewritten
> it and got errors. I want a conditional in this program a choice between
> 2
> options and only one works. I don't think I need the strcpy either.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> main(){
> char pass[10]="ded", pass2[10];
> printf("Authorization ");
> fflush(stdout);
> fgets(pass,10,stdin);
> strcpy(pass2,pass);
> if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
> else
> printf("error"); exit(EXIT_FAILURE);}
>
>




--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      03-01-2008
Karthigan Srinivasan said:

> This should work.


<snip>

> gets(pass2);


This is a really, really, reall bad idea. Never, *ever* use gets(), because
it has no way to protect your buffer against being overrun by excessive
data.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008
> If your current version is similar to the original, then the problem
> you have is that fgets retains the terminating newline (the character
> that marks the end of a line of input). You need either to remove
> this newline from pass2 or test for strcmp(pass2, "ded\n") == 0.
>

Wow. Thanks Ben. I didn't know strcmp took a literal string. So much for
my knowledge.

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      03-01-2008

"Morris Dovey" <> wrote in message
news:...

> I'm assuming that you intended for the password to be "ded"...


Yes.

> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> int main(void)
> { char pass[10]="ded", pass2[10];
>
> printf("Authorization ");
> fflush(stdout);
> fgets(pass2,10,stdin);
> if(strcmp(pass,pass2))
> { printf("error");
> exit(EXIT_FAILURE);
> }
> printf("success");
> return 0;
> }
>
> If you terminate the program immediately for an incorrect
> password, you won't need an 'else' for the success path.
>



 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-01-2008
"Bill Cunningham" <> writes:
> I have been having fits with this function.
> Can anyone help me I think I need to use if and else if but I have rewritten
> it and got errors. I want a conditional in this program a choice between 2
> options and only one works. I don't think I need the strcpy either.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> main(){
> char pass[10]="ded", pass2[10];
> printf("Authorization ");
> fflush(stdout);
> fgets(pass,10,stdin);
> strcpy(pass2,pass);
> if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
> else
> printf("error"); exit(EXIT_FAILURE);}


Do you pay a per-character fee for using whitespace in your source
code?

I think your code layout is making it difficult for you to see what
your program is actually doing. It certainly makes it difficult for
me to read it. In particular, your style of hiding each closing brace
character '}', combined with your insistence on putting multiple
statements on a line, is probably the cause of your current confusion.
Using only a single column for indentation also makes your code
difficult to read.

Here's your program again, with no changes other than the addition of
whitespace (I would make several other changes as well, but I haven't
done so here). It should be enough to show you what the problem is.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
char pass[10] = "ded", pass2[10];
printf("Authorization ");
fflush(stdout);
fgets(pass, 10, stdin);
strcpy(pass2, pass);
if (strcmp(pass, pass2) == 0) {
printf("success");
exit(0);
}
else
printf("error");
exit(EXIT_FAILURE);
}

Code layout matters.

--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-01-2008
Keith Thompson <kst-> writes:
> "Bill Cunningham" <> writes:
>> I have been having fits with this function.
>> Can anyone help me I think I need to use if and else if but I have
>> rewritten it and got errors. I want a conditional in this program a
>> choice between 2 options and only one works. I don't think I need
>> the strcpy either.
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>> #include <string.h>
>>
>> main(){
>> char pass[10]="ded", pass2[10];
>> printf("Authorization ");
>> fflush(stdout);
>> fgets(pass,10,stdin);
>> strcpy(pass2,pass);
>> if(strcmp(pass,pass2)==0) {printf("success"); exit(0);}
>> else
>> printf("error"); exit(EXIT_FAILURE);}

>
> Do you pay a per-character fee for using whitespace in your source
> code?
>
> I think your code layout is making it difficult for you to see what
> your program is actually doing. It certainly makes it difficult for
> me to read it. In particular, your style of hiding each closing brace
> character '}', combined with your insistence on putting multiple
> statements on a line, is probably the cause of your current confusion.
> Using only a single column for indentation also makes your code
> difficult to read.
>
> Here's your program again, with no changes other than the addition of
> whitespace (I would make several other changes as well, but I haven't
> done so here). It should be enough to show you what the problem is.
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
>
> main()
> {
> char pass[10] = "ded", pass2[10];
> printf("Authorization ");
> fflush(stdout);
> fgets(pass, 10, stdin);
> strcpy(pass2, pass);
> if (strcmp(pass, pass2) == 0) {
> printf("success");
> exit(0);
> }
> else
> printf("error");
> exit(EXIT_FAILURE);
> }
>
> Code layout matters.


My apologies, I didn't read the code closely enough.

I has assumed that you wanted both statements:
printf("error");
exit(EXIT_FAILURE);
to be in the else clause, which would require braces surrounding both
of them.

Since the if clause:
printf("success");
exit(0);
always exits, it doesn't matter *in this particular case*. The
behavior of the program is the same whether the exit(EXIT_FAILURE); is
within the else clause, or following and separate from the entire
if/else statement.

But your original code almost completely obscures this point. Your
problem isn't what I thought it was, but your coding style still makes
your code difficult to read. And IMHO the exit(EXIT_FAILURE); clause
*should* be part of the else clause.

Here's another version of your program, again with no changes other than
whitespace:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){char pass[10]="ded",pass2[10];printf("Authorization "
);fflush(stdout);fgets(pass,10,stdin);strcpy(pass2 ,pass);if(
strcmp(pass,pass2)==0){printf("success");exit(0);} else printf(
"error");exit(EXIT_FAILURE);}

It's not as legible as your original version, but it's not all that
much worse.

Fixing this problem (and, among other things, showing that you can
accept good advice) will make it more likely that my response to your
next question will be anything more than "Please clean up this mess if
you expect me to read it".

Furthermore, all you said about your problem is that you "got errors".
Telling us *what* errors you got, as well as what you expected the
program to do, would make it much easier to help you.

--
Keith Thompson (The_Other_Keith) <kst->
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Function versus pointer to function, in context of std::function,huh? Alf P. Steinbach C++ 10 07-27-2011 05:51 AM
Function pointer to void function and int function Giannis Papadopoulos C Programming 5 09-05-2005 09:06 PM
How override ALL function calls? (Is there a "function call function"?) seberino@spawar.navy.mil Python 2 08-01-2005 12:38 PM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM
Passing a C++ object's member function to a C function expecing a function pointer! James Vanns C++ 7 01-21-2004 02:39 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57