晚上好,我有两个函数,每个函数都接受一个 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
}
现在我尝试了 strstr,strcmp,但它们都不起作用。想尝试使用 memcmp,但我没有静态长度。由于我需要比较 char* 和 char 以及 char* 和 char*,所以我需要两个解决方案,对吧?
那么,如何以最短的方式比较这些指针(实际上是指向的对象)?
谢谢。
编辑
感谢 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...
任何建议可能会有所帮助...(嗯,用于比较的外部函数?)
< p > < em >建议1(由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;
}
strcmp
。 - cnicutar