将指针指针与字符进行比较?

时间:2015-08-15 10:30:09

标签: c arrays pointers

我有一个字符数组。指针指向该字符数组。另外,另一个指针指向该指针,即它是指向指针的指针。现在,我需要将指针指针与字符文字进行比较,比如" a"。

我尝试过strcmp,但它没有用。

我该怎么做?

    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");

1 个答案:

答案 0 :(得分:4)

如果我理解正确您正在谈论以下

#include <string.h>

//...

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

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

或者

#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" );

如果您需要比较单个字符,则可以编写

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

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

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

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