C comparing pointers (with chars)

↘锁芯ラ 提交于 2019-11-30 18:31:33

You need to use strcmp. Not seeing how you tried to use it, this is how you should use it:

char *someother_char = "a";
char *pointer = "a";

if (strcmp(pointer, someother_char) == 0) { // match!
}
else { // not matched 
}

to then do the comparison with a char, you have to promote to a char*:

char *someother_char1;
char test = 'a';
char *pointer = "a";

strncpy((char*)test,someother_char1,sizeof(test));

if (strcmp(pointer, someother_char1) == 0) { // match!
}
else { // not matched 
}

if you want to use the char array then you have to de-reference:

char char_array[255];
// don't forget to fill your array
// and add a null-terminating char somewhere, such as char_array[255] = '\0';
char *ptr_somechar = &char_array[0];
char *pointer = "a";

if (strcmp(pointer, ptr_somechar) == 0) { // match!
} else { // not matched
}

Well right off the bat, if you want to compare the pointees, you need to dereference them. This means to compare the actual char value, you'll have to call

if (*pointer == someother_char)

However this will only compare the first char in the array, which is probably not what you want to do.

To compare the whole thing strcmp should work

char* someother_str = "hello strstr";
if(strcmp(pointer, someother_str) == 0) {
    // do something
}

Make sure your other string is declared as a char*

More info: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

Edit: as per your comment. comparing char* and char doesn't really make sense. One is a character value, the other is an address in memory. Do do so, you can either dereference the char* or reference the value variable.

char c;
char* ptr;

// dereference ptr
if ( c == *ptr ) {
   ...
}

// reference the value
if ( &c == ptr ) {

}

The first method checks if the values are the same. The second checks if ptr is in fact pointing to the memory containing c ie. is ptr a pointer to c

Hope that helps

Use function srtncmp no srtcmp.

int res = strncmp(str, "¿Cuál es tu nombre? ", 100);

See the next link

compare strings

Strings are null terminated. When you use such kind of strings, it's not a good idea to mixing with other memory copy functions. Once you do the memcpy operation, please note that your destination string will not be null terminated. memcmp is a fast operations. Otherwise yo can simply loop through each character and quit upon finding a difference. To use strcmp, please make sure that both the strings are null terminated. Otherwise it will lead to some crash.

I suggest you to use string functions like strcmp,strlen, strcpy to deal with strings because for that it's actually implemented.

You can't compare two pointers unless both pointers are referring to same memory location. Pointer is just a address to a memory location. What you really want to do is that, to compare the contents rather than compare the address where it's stored. So please use strcmp but again I warn you make sure that it's null terminated.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!