C语言 C 比较指针(带字符)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7125697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C comparing pointers (with chars)
提问by cat9
Good evening, I have 2 functions and each of them accepts as argument a pointer to char:
晚上好,我有 2 个函数,每个函数都接受一个指向 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,strcmpetc... doesn't work. Wanted to try memcmpbut I don't have static len. As I have to compare char* to charand char* to char*I would be needing two solutions right?
现在我已经尝试过strstr、strcmp等...不起作用。想尝试memcmp但我没有静态 len。因为我必须将char* 与 char和char* 与 char* 进行比较,所以我需要两种解决方案,对吗?
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:
感谢 wallacer 和 Code Monkey 现在用于 char* 到 char 的比较,我使用以下内容:
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)
Suggestion1(by Code Monkey)
#include <stdio.h>
int main(void) {
char tempchar[255];
tempchar[0] = 'a';
tempchar[1] = 'b';
tempchar[2] = 'char *someother_char = "a";
char *pointer = "a";
if (strcmp(pointer, someother_char) == 0) { // match!
}
else { // not matched
}
';
char *ptr_char;
ptr_char = &tempchar[0];
printf("%s", ptr_char);
return 0;
}
回答by
You need to use strcmp. Not seeing how you tried to use it, this is how you should use it:
您需要使用strcmp. 没有看到您如何尝试使用它,这是您应该如何使用它:
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
}
to then do the comparison with a char, you have to promote to a char*:
然后与 a 进行比较char,您必须提升为 a char*:
char char_array[255];
// don't forget to fill your array
// and add a null-terminating char somewhere, such as char_array[255] = 'if (*pointer == someother_char)
';
char *ptr_somechar = &char_array[0];
char *pointer = "a";
if (strcmp(pointer, ptr_somechar) == 0) { // match!
} else { // not matched
}
if you want to use the chararraythen you have to de-reference:
如果要使用char数组,则必须取消引用:
char* someother_str = "hello strstr";
if(strcmp(pointer, someother_str) == 0) {
// do something
}
回答by wallacer
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
好吧,如果你想比较指针对象,你需要取消引用它们。这意味着要比较实际的字符值,您必须调用
char c;
char* ptr;
// dereference ptr
if ( c == *ptr ) {
...
}
// reference the value
if ( &c == ptr ) {
}
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
比较 strcmp 应该工作的整个事情
int res = strncmp(str, "?Cuál es tu nombre? ", 100);
Make sure your other string is declared as a char*
确保您的另一个字符串被声明为 char*
More info: http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
更多信息: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* 和 char 并没有什么意义。一个是字符值,另一个是内存中的地址。这样做,您可以取消引用 char* 或引用 value 变量。
##代码##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
第一种方法检查值是否相同。第二个检查 ptr 实际上是否指向包含 c 的内存,即。ptr 是指向 c 的指针
Hope that helps
希望有帮助
回答by Ronald
Use function srtncmp no srtcmp.
使用函数 srtncmp 无 srtcmp。
##代码##See the next link
见下一个链接
回答by sarat
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.
字符串以空字符结尾。当您使用此类字符串时,与其他内存复制功能混合使用不是一个好主意。一旦您执行了 memcpy 操作,请注意您的目标字符串不会以空字符结尾。memcmp 是一个快速的操作。否则你可以简单地遍历每个字符并在发现差异时退出。要使用 strcmp,请确保两个字符串都以空字符结尾。否则会导致一些崩溃。
I suggest you to use string functions like strcmp,strlen, strcpy to deal with strings because for that it's actually implemented.
我建议你使用 strcmp、strlen、strcpy 之类的字符串函数来处理字符串,因为它实际上已经实现了。
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.
除非两个指针都指向相同的内存位置,否则您无法比较两个指针。指针只是指向内存位置的地址。你真正想要做的是,比较内容而不是比较它存储的地址。所以请使用 strcmp 但我再次警告你确保它是空终止的。

