C comparing pointers (with chars)

前端 未结 4 1054
我在风中等你
我在风中等你 2021-01-04 08:28

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_ch         


        
4条回答
  •  抹茶落季
    2021-01-04 08:58

    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.

提交回复
热议问题