do I need strcmp ?

J

John Gordon

In said:
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");
 
M

Martin Ambuhl

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 */
}
 
B

Bill Cunningham

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
 
B

Bill Cunningham

John said:
In <[email protected]> "Bill Cunningham"
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
 
N

Nick Keighley

     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
 
K

Keith Thompson

Nick Keighley said:
     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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top