简体   繁体   English

将指针与带有字符的指针进行比较?

[英]Comparing a pointer to a pointer with a character?

I have a character array. 我有一个字符数组。 A pointer points to that character array. 指针指向该字符数组。 Also, another pointer points to that pointer ie it is a pointer to a pointer. 同样,另一个指针指向该指针,即它是指向指针的指针。 Now, I need to compare the pointer to the pointer with a character literal, say "a". 现在,我需要将指针与带有字符文字的指针进行比较,例如“ a”。

I tried strcmp but it didn't work. 我尝试了strcmp,但是没有用。

How can I do this? 我怎样才能做到这一点?

    char arr[1000];
    char *ptr,*ptr1,**curr;
    ptr=arr;        //pointer pointer to that array
    curr=&ptr;      //pointer to a pointer
    if(**curr=='.') //doesn't work
        printf("Some code");

If I have understood correctly you are speaking about the following 如果我理解正确,那么您在谈论以下内容

#include <string.h>

//...

char arr[1000] = "a";
char *ptr = arr;
char **curr = &ptr;

if ( strcmp( *curr, "a" ) == 0 ) puts( "They are equal" );

Or 要么

#include <string.h>

//...

char arr[1000] = "abcd";
char *ptr = arr;
char **curr = &ptr;
char *ptr2 = "ab";

if ( strncmp( *curr, ptr2, strlen( ptr2 ) ) == 0 ) puts( "They are equal" );

If you need to compare a single character then you can write 如果您需要比较单个字符,则可以编写

char arr[1000] = "a";
char *ptr = arr;
char **curr = &ptr;

if ( **curr == 'a' ) puts( "They are equal" );

or 要么

char arr[1000] = "ab";
char *ptr = arr;
char **curr = &ptr;

if ( ( *curr )[1] == 'b' ) puts( "They are equal" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM