in Technology by
How can we do Pointer comparison in C?

1 Answer

0 votes
by

Like the pointer subtraction, we can also compare the pointer but pointer should be pointed to the same array or memory block.

See the below example;

char acBuffer[10] = “aticleworld”;
char *pc1, *pc2;

 

Assigning address of first and third elements of acBuffer1 to pc1 and pc2.

pc1 = &acBuffer[0]; // Address of first element
pc2 = &acBuffer[2]; // Address of third element
Now we can compare the pointers if required.
if( pc1 > pc2) // Legal statement
{
}

 

But pointers comparison is invalid when pointers point to the 2 different memory blocks.

See the below example;

char acBuffer1[10] = “aticle”;
char acBuffer2[10] = “world”;
char *pc1, *pc2;

 

Assigning the address of acBuffer1 to pc1 and acBuffer2 to the pc2.

pc1 = acBuffer1;
pc2 = acBuffer2;

 

In that situation pointer comparison is invalid.

if( pc1 > pc2) // illegal statement
{
}

Related questions

0 votes
    What is a normalized pointer, how do we normalize a pointer?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    Can we have a volatile pointer?...
asked Jan 22, 2021 in Technology by JackTerrance
+1 vote
    Which operator can be used to access union elements if union variable is a pointer variable in C Programming ?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    Can a pointer access the array in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    Which built-in function can be used to move the file pointer internally in C Programming?...
asked Nov 9, 2020 in Technology by JackTerrance
+1 vote
    What are the ways to a null pointer that can be used in the C programming language?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    Can we have a volatile pointer?...
asked Jan 23, 2021 by JackTerrance
0 votes
    How is a Program increment a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    How to declare a pointer to a function in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What happens when we invoke a method on a nil pointer?...
asked Nov 10, 2020 in Technology by JackTerrance
0 votes
    What is Indirection and Increment/Decrement operators with a pointer in C?...
asked Jan 24, 2021 in Technology by JackTerrance
0 votes
    Which type of pointer is the most convenient way of storing the raw address in C programming?...
asked Jan 23, 2021 in Technology by JackTerrance
0 votes
    What is the use of a double pointer (pointer to pointer) in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the advantage of a void pointer in C?...
asked Jan 22, 2021 in Technology by JackTerrance
0 votes
    What is the usage of the NULL pointer in C?...
asked Jan 21, 2021 in Technology by JackTerrance
...