Memory Address Comparison

How would I compare two memory addresses from a char array with a fixed size? Suppose I have two pointers, each of which points to a different memory location in the array:

char *ptr1; //points to a memory address in the array;
char *ptr2; //points to another memory address in the array;

If I do printf("%p\n%p\n", ptr1, ptr2);, then it will print the memory addresses as hexadecimal.

output:
0x601240
0x601274

how would I store them in variables and compare so that I can determine which memory address will be the first in the array.

Another question: Instead of% p, if I do% d to print the memory address, I get:

output:
6296128
6296180

Are these valid memory addresses (I mean, is it safe to use)?

+4
source share
3 answers

- . ( ),

if (ptr1 < ptr2)

, %d ( , ), %p void *:

printf("%p\n%p\n", (void *)ptr1, (void *)ptr2);
+11

0x601240 - , 6296128 - . , 0x601274 6296180. 0x , .

C/++ ptr1 ptr2 < >, , . ( , printf.)

+3

. , - .

, ( ..), . , , . , , , , ...

+2

All Articles