12

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;
}
hakre
  • 193,403
  • 52
  • 435
  • 836
cat9
  • 157
  • 1
  • 2
  • 8

4 Answers4

22

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
}
  • only answers half of my question ... how to compare char* with char is problem... – cat9 Aug 19 '11 at 18:32
  • Now thats interesting, I've tried this char *ptr = NULL and then in the code ptr = &someother_char; which gave error char(*)[255] to char*. Anyway its working now (w/o referecing... still testing in response to wallacer above) but this is sexy answer **Code Monkey**. – cat9 Aug 19 '11 at 19:18
  • @cat9: oops, it is actually `&char_array[0]` and you don't need to set it to `null`. See here for an example program : http://ideone.com/O4bKt –  Aug 19 '11 at 19:35
  • @cat9: If this is what helped you solve your problem, don't forget to click the accept checkmark next to my answer. –  Aug 19 '11 at 19:46
  • 1
    Note that `char *pointer = "a"` is really evil and only allowed for backwards comp reasons to C. strcmp works perfectly fine with `const char array[] = "a"` and that's imo the clearest form. But at least don't use the `const`. – Voo Aug 19 '11 at 20:25
  • but that way Voo you don't get pointer and you have to act like &array[0] no? – cat9 Aug 19 '11 at 22:37
  • if someone sees this one too, is it comon sense to avoid missing \0 and use `char hax[255] = {0};`? This way I'm also wasting memory or no? – cat9 Aug 19 '11 at 22:39
  • @cat9: You can pre-fill your array with zero, yes. However, I prefer to ensure that the null-terminating char is always there because otherwise you could end up with a potential buffer overflow or vulnerability. –  Aug 20 '11 at 00:29
1

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

wallacer
  • 12,855
  • 3
  • 27
  • 45
  • Okay this is what I have in mind but thats only half of the question. What if I have to compare char* and char? PS Good thing that I recalled how dereferecing works and that only first "letter" would be compared... – cat9 Aug 19 '11 at 18:33
  • Well above still holds... I want to compare pointee of char* to some other char. Your code is correct but not an answer coz I will just compare first "letter". – cat9 Aug 19 '11 at 18:41
  • @cat9 comparing a char* to a char doesn't make sense. In fact, it will always fail I think, since a char is generally 8 bits and memory addresses will generally either be 32 or 64 bits depending on your system. a char can only be in the range 0 - 255 whereas memory addresses occupy a much much larger number range – wallacer Aug 19 '11 at 18:41
  • use the strcmp solution I provided. it compares the whole string – wallacer Aug 19 '11 at 18:42
  • char* ptr = "omg"; char somechar[4] = "omg"; strcmp(ptr,somechar)? Will this even compile? Can I use char *ptr_somechar = somechar and then proceed with strcmp? – cat9 Aug 19 '11 at 18:46
  • if you want a pointer to a char, use char* ptr_somechar = &somechar; – wallacer Aug 19 '11 at 18:52
  • Damn, isn't somechar perse giving adr of first "letter" in array, no problemo I will reference &somechar, just asking. I already made it w/o reference & so I will report does it work my way. – cat9 Aug 19 '11 at 18:56
  • Thanks for your time, I have used char *ptr = NULL; in declaration then I just use ptr = someother_char; and strcmp... any suggestions? (& referecing failed on me this way... maybe I have to do all in declaration like you) Thanks again **wallacer**. – cat9 Aug 19 '11 at 19:22
0

Use function srtncmp no srtcmp.

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

See the next link

compare strings

Ronald Coarite
  • 4,460
  • 27
  • 31
-1

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.

sarat
  • 10,512
  • 7
  • 43
  • 74
  • Like I said ... pointees are needed to be compared... re-read whole thread please sarat if you have time. Thanks – cat9 Aug 19 '11 at 18:39
  • In my understanding you need only to check the first character in the buffer. That can be done either by accessing pstr[0] or *pstr i.e. if(pstr[0] == 'Y') // exit – sarat Aug 19 '11 at 18:57
  • See that's not possiblec cat9 because pointers are just containing a location value not really the content. You've to deference it to see the content. When you compare a pointer against anything, you're simply comparing a memory address with other. At the same time, using *ptr will derefer the memory location and get the content in that location and comapare. e.g if( ptr == 'c' )// Wrong if( ptr == ptr1 ) // memory location comparison. rarely used if( *ptr == 'c' )// correct dereferencing the pointer and compare the data. – sarat Aug 19 '11 at 19:08
  • how to dereference and obtain complete array(string)? – cat9 Aug 19 '11 at 19:34