Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > do I need strcmp ?

Reply
Thread Tools

do I need strcmp ?

 
 
John Gordon
Guest
Posts: n/a
 
      11-04-2011
In <4eb4605c$0$20319$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> I am doing a comparison here and the code works and then doesn't work.
> My goal is to start a string to be recognized by a function only if it
> starts with a +. Kind of like cpp doesn't recognize preprocessor commands
> unless # is present.


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


> int main()
> {
> char *plus;
> char *string = "+ hello there\n";
> plus = strchr(string, '+');
> if ((int) plus[0] == '+')
> printf("ok\n");
> if ((int) plus[0] != '+')
> printf("no\n");
> }


> This code as it is works marvelously when finding + in a string. If
> something other than + is used, I do not get no but a segmentation fault. Is


That's because strchr returns NULL when the target character is not found,
which causes 'plus' to have a null value. Then, when you attempt to
look at plus[0], the program crashes.

> the only way around this to use strcmp to see if the first character of the
> string is + or not? Can the code be changed to accomplish what I want
> without using strcmp?


Using strchr seems like overkill in this case. You don't even need the
'plus' variable. Just look for the character directly:

char *string = "+hello there\n";

if(string[0] == '+')
printf("ok\n");
else
printf("no\n");

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      11-04-2011
On 11/4/2011 6:59 PM, Bill Cunningham wrote:
> I am doing a comparison here and the code works and then doesn't work.
> My goal is to start a string to be recognized by a function only if it
> starts with a +.


If the string is in, say, an array named 'command',
just check as follows:

if (command[0] == '+') {
/* do stuff */
}
else {
/* don't do stuff */
}


 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-04-2011
I am doing a comparison here and the code works and then doesn't work.
My goal is to start a string to be recognized by a function only if it
starts with a +. Kind of like cpp doesn't recognize preprocessor commands
unless # is present.

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

int main()
{
char *plus;
char *string = "+ hello there\n";
plus = strchr(string, '+');
if ((int) plus[0] == '+')
printf("ok\n");
if ((int) plus[0] != '+')
printf("no\n");
}

This code as it is works marvelously when finding + in a string. If
something other than + is used, I do not get no but a segmentation fault. Is
the only way around this to use strcmp to see if the first character of the
string is + or not? Can the code be changed to accomplish what I want
without using strcmp?

Bill


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-04-2011
John Gordon wrote:
> In <4eb4605c$0$20319$(E-Mail Removed)> "Bill Cunningham"
> <(E-Mail Removed)> writes:
>
>> I am doing a comparison here and the code works and then doesn't
>> work. My goal is to start a string to be recognized by a function
>> only if it starts with a +. Kind of like cpp doesn't recognize
>> preprocessor commands unless # is present.

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

>
>> int main()
>> {
>> char *plus;
>> char *string = "+ hello there\n";
>> plus = strchr(string, '+');
>> if ((int) plus[0] == '+')
>> printf("ok\n");
>> if ((int) plus[0] != '+')
>> printf("no\n");
>> }

>
>> This code as it is works marvelously when finding + in a string. If
>> something other than + is used, I do not get no but a segmentation
>> fault. Is

>
> That's because strchr returns NULL when the target character is not
> found,
> which causes 'plus' to have a null value. Then, when you attempt to
> look at plus[0], the program crashes.
>
>> the only way around this to use strcmp to see if the first character
>> of the string is + or not? Can the code be changed to accomplish
>> what I want without using strcmp?

>
> Using strchr seems like overkill in this case. You don't even need
> the 'plus' variable. Just look for the character directly:
>
> char *string = "+hello there\n";
>
> if(string[0] == '+')
> printf("ok\n");
> else
> printf("no\n");


Thanks that NULL completely slipped my mind.

Bill


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-06-2011
On Nov 4, 10:36*pm, Martin Ambuhl <(E-Mail Removed)> wrote:
> On 11/4/2011 6:59 PM, Bill Cunningham wrote:
>
> > * * *I am doing a comparison here and the code works and then doesn't work.
> > My goal is to start a string to be recognized by a function only if it
> > starts with a +.

>
> If the string is in, say, an array named 'command',
> just check as follows:
>
> * * if (command[0] == '+') {
> * * * */* do stuff */
> * * }
> * * else {
> * * * */* don't do stuff */
> * * }


first ensuring that command contains at least one character
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-06-2011
Nick Keighley <(E-Mail Removed)> writes:
> On Nov 4, 10:36*pm, Martin Ambuhl <(E-Mail Removed)> wrote:
>> On 11/4/2011 6:59 PM, Bill Cunningham wrote:
>>
>> > * * *I am doing a comparison here and the code works and then doesn't work.
>> > My goal is to start a string to be recognized by a function only if it
>> > starts with a +.

>>
>> If the string is in, say, an array named 'command',
>> just check as follows:
>>
>> * * if (command[0] == '+') {
>> * * * */* do stuff */
>> * * }
>> * * else {
>> * * * */* don't do stuff */
>> * * }

>
> first ensuring that command contains at least one character


If command is an array object, then no such check is necessary.
If it contains an empty string, command[0] is '\0'. If it's a
zero-length array, then, well, the program must be in a language
other than C that supports such things.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"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
segfault when strcmp Robert Mens C Programming 6 10-23-2003 02:53 AM
strcmp muser C++ 6 10-09-2003 08:18 AM
strcmp problem Shane Peck C++ 6 09-22-2003 05:44 PM
strcmp but with '\n' as the terrminator Allan Bruce C Programming 53 07-30-2003 07:38 PM
please help with strcmp() Andrej Hocevar C Programming 3 07-19-2003 04:41 PM



Advertisments