简体   繁体   中英

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".

I tried strcmp but it didn't work.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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