Go Down

Topic: strncmp question (Read 902 times) previous topic - next topic

alfiesty

I have the line            if(strncmp(pch,"=1",1) == 0)                in my code. pch is "Chapter 1, page 23". As I understand the if should return a 0.  It doesn't,why?

Jim
8000ft above the average

guix

#1
Oct 18, 2012, 03:48 am Last Edit: Oct 18, 2012, 03:52 am by guix Reason: 1
Well no, it shouldn't return 0 because you are comparing only the first character, also there is no "=" sign in pch :)

http://www.cplusplus.com/reference/clibrary/cstring/strncmp/

PaulS

Quote
also there is no "=" sign in pch

Specifically, the FIRST character is not '='.

The strncmp() function compares the first n characters. There are other functions, like strstr() that look for a string anywhere inside another string.

Go Up