Ç比较指针(用字符) [英] C comparing pointers (with chars)

查看:130
本文介绍了Ç比较指针(用字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

晚上好,我有2个功能,他们每个人的接受作为参数字符指针:

 字符指针[255];
func1的(字符*指针)
{
...
的memcpy(指针,some_char,strlen的(东西));
返回;
}
FUNC2(字符*指针)
{
...
如果(指针== someother_char)出口(0); //失败
//我也有
如果(指针== someother_pointer2char); //失败
}

现在我已经试过的strstr,STRCMP 等等不起作用。想尝试的 memcmp 但是我没有静态LEN。因为我有比较的char *为char 的char *为char * 我将需要两个解决方案吧?

那么,如何在最短的方式将这些指针(实际上是指针对象)相比较?

感谢。

E D I T

感谢wallacer和code猴子现在的char *为char比较,我使用以下内容:

  func1的(字符* PTR){
焦炭someother_char [255];
字符* ptr_char = NULL; //我要strcmp的几个值,所以这就是为什么我第一次初始化
...
ptr_char = someother_char;
如果(STRCMP(PTR,ptr_char)== 0)// GTFO,它不会...
...
ptr_char = some2nd;
如果(strcmp的...

任何建议,也许...(嗯比较外部函数?)

Suggestion1(由code猴)

 的#include<&stdio.h中GT;诠释主要(无效){
    焦炭tempchar [255];
    tempchar [0] ='A';
    tempchar [1] ='B';
    tempchar [2] ='\\ 0';
    字符* ptr_char;
    ptr_char =安培; tempchar [0];
    的printf(%S,ptr_char);
    返回0;
}


解决方案

您需要使用 STRCMP 。没有看到你如何试图使用它,这是你应该如何使用它:

 的char * someother_char =A;
字符*指针=A;如果(STRCMP(指针,someother_char)== 0){//比赛!
}
其他{//不匹配
}

要那么做一个字符,你必须提升到一个的char * 的比较:

 的char * someother_char1;
焦炭测试='A';
字符*指针=A;函数strncpy((字符*)测试,someother_char1,sizeof的(测试));如果(STRCMP(指针,someother_char1)== 0){//比赛!
}
其他{//不匹配
}

如果你想使用字符阵列的,那么你必须去参考:

 字符CHAR_ARRAY [255];
//不要忘记填写您的数组
//并添加一个空终止符的地方,如CHAR_ARRAY [255] ='\\ 0';
字符* ptr_somechar =安培; CHAR_ARRAY [0];
字符*指针=A;如果(STRCMP(指针,ptr_somechar)== 0){//比赛!
}其他{//不匹配
}

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
}

这篇关于Ç比较指针(用字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆