tags:

views:

73

answers:

3

Im trying to create a C based string menu where a user inputs a command and then a block of code runs.

Whatever i do the conditional is never true:

char *input= "";
fgets(input, 50, stdin);
printf("%s",input);
printf("%d",strcmp( input,"arrive\0"));
if(strcmp( input,"arrive\0")==0){....

Im fairly new to c and am finding strings really annoying.

What am i doing wrong?

Note: current code crashes my program :(

+5  A: 

Why strcmp always return non 0:

strcmp will return 0 only when the strings are identical. As for why it's evaluating to different always. It is because fgets puts a newline character at the end of your input buffer before the null termination.

/*Will print 0 if you type in arrive<enter>*/
printf("%d",strcmp( input,"arrive\n"));

Why your program crashes:

Another problem is that input should be a char buffer. Like so: char input[1024]; Currently you have input as a pointer to a null terminated string (which is read only memory)


Friendly suggestion:

Also don't put the null terminated \0 inside the string literals. It is implied automatically when you use a string literal. It doesn't matter to double null terminate as far as strcmp is concerned, but it may cause problems elsewhere in your future programs. And people will wonder why you're doing double null termination.

Brian R. Bondy
Thanks for the thorough answer
ahref
@ahref: No prob :)
Brian R. Bondy
+1  A: 

Try replacing the first line with

char input[50];
memset(input, 0, sizeof(input));

Edit: However, the real problem why strcmp doesn't return 0 is you have to "trim" the string read from fgets, which in most cases, includes a newline character.

shinkou
Or just `char input[50] = {0}` instead of wasting cycles `memset()`-ing everything.
Chris Lutz
Why the memset for a buffer that will be overwritten by `fgets` anyway?
msw
It's just a convention to prevent C learners from getting caught with common pitfalls.
shinkou
@shinkou - I initially suggested it too, however fgets() is guaranteed to null terminate the buffer. Minor nits, however.
Tim Post
+3  A: 

Try :

#define BUFF_LEN 256

char input[BUFF_LEN];

fgets(input, BUFF_LEN, stdin);

What you have , *input is a pointer to an address of memory that has not been allocated, hence can not be used by your program. The result of using it as you are is undefined, but usually leads to a segmentation fault. If you want to access it as a pointer, you will first need to allocate it:

char *input = malloc(BUFF_LEN * sizeof(char *) + 1);

... of course, test that for failure (NULL) then free() it after you are done using it.

Edit:

At least according to the single UNIX specification, fgets() is guaranteed to null terminate the buffer. Its not necessary to initialize input[].

As others have said, it is not necessary to include null / newlines when using strcmp().

I also strongly, strongly advise you to get used to using strncmp() now, while beginning to avoid many problems down the road.

Tim Post
the count fgets takes in includes the null character, so there is no need for the `- 1`.
Matthew Flaschen
@Matthew - Correct, edited. Force of habit :)
Tim Post