开发者

C comparing pointers (with chars)

开发者 https://www.devze.com 2023-03-29 14:41 出处:网络
Good evening, I have 2 functions and each of them accepts as argument a pointer to char: char pointer[255];

Good evening, I have 2 functions and each of them accepts as argument a pointer to char:

char pointer[255];
func1(char* pointer)
{
...
memcpy(pointer,some_char,strlen(something));
return;
}
func2(char* pointer)
{
...
if (pointer==someother_char) exit(0); //FAILs
//also I have
if(pointer==someother_pointer2char); // FAILs
}

Now I've tried strstr,strcmp etc... doesn't work. Wanted to try memcmp but I don't have static len. As I have to compare开发者_运维技巧 char* to char and char* to char* I would be needing two solutions right?

So, how to compare these pointers (actually pointees) in shortest possible way?

Thanks.

E D I T

Thanks to wallacer and Code Monkey now for char* to char comparison I use following:

func1(char* ptr){
char someother_char[255];
char *ptr_char = NULL; //I have to strcmp a few values so this is why I initialize it first
... 
ptr_char = someother_char;
if (strcmp(ptr,ptr_char) == 0) //gtfo and it does...
...
ptr_char = some2nd;
if(strcmp...

Any suggestions maybe... (hmm external function for comparing?)

Suggestion1(by Code Monkey)

#include <stdio.h>

int main(void) {
    char tempchar[255];
    tempchar[0] = 'a';
    tempchar[1] = 'b';
    tempchar[2] = '\0';
    char *ptr_char;
    ptr_char = &tempchar[0];
    printf("%s", ptr_char);
    return 0;
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号