In the following table you can se the operators that can be used with pointers.
Operator | Name | Meaning |
---|---|---|
* | Used to declare a pointer | |
* | Dereference | Used to dereference a pointer |
-> | Point-to | Used to access fields of a structure referenced by a pointer |
+ | Addition | Used to increment a pointer |
– | Subtraction | Used to decrement a pointer |
== or != | Equality or inequality | Compares two pointers |
> or >= or < or <= | Greater than, greater than or equal, etc. | Compares two pointers |
(data type) | Cast | To change the type of pointer |
Pointer Arithmetic
Adding an integer to a pointer
Adding an integer to a pointer is common and useful operation. The table “Data type sizes below shows the common sizes in bytes of given data type in most systems.
Data type | Size in Bytes |
---|---|
byte | 1 |
char | 1 |
short | 2 |
int | 4 |
long | 8 |
float | 4 |
double | 8 |
#include <stdio.h>
int main(void)
{
int vector[] = {28, 41, 7};
int *ptr;
ptr = vector;
printf("%d\n", *ptr);
printf("%d\n", ptr); // correctly %p
ptr += 1;
printf("%d\n", *ptr);
printf("%d\n", ptr); // correctly %p
ptr += 1;
printf("%d\n", *ptr);
printf("%d\n", ptr); // correctly %p
}
Terminal$ gcc PointerArithmetic.c
Terminal$ ./a.out
28
1431783788
41
1431783792
7
1431783796
As in example above, you can see, that we have an array of integers called vector. As we can see the pointer ptr is pointed to the first element of an array. In our case the first element is located in byte 1431783788 and the second element in 4 bytes more. To move to the second element we use the pointer arithmetic ptr = ptr + 1.
Pointers to void and addition
With most compilers it is possible to perform the arithmetic on a pointer to void. However, trying to add one to a pointer to void can result in syntax error. It is not standard C so if a compiler allows it, the result can be a warning. Most compilers will treat the data type void as if it has a size of 1 byte so the address would be incremented by 1.
Subtracting an integer from a pointer
ptr--; // or ptr = ptr - 1;
Subtracting an integer from a pointer works the same as adding an integer to a pointer. So if we have an array of integer, four would be subtracted from the address in the example above.
Subtracting two pointers
When you subtract on pointer from another, you get the difference between their addresses. This difference can be useful for determining the order of elements in an array.
#include <stdio.h>
int main(void)
{
int vector[] = {28, 41, 7};
int *ptr0;
int *ptr1;
int *ptr2;
ptr0 = vector;
ptr1 = vector + 1;
ptr2 = vector + 2;
printf("%ld\n", ptr2 - ptr0); // we get the result 2
printf("%ld\n", ptr1 - ptr0); // we get the result 1
}
As the result of the difference between two pointers we get the number of “units” as seen in the code above. The type ptrdiff_t is used to represent the difference of two pointers and is defined in the header file stddef.h.
Comparing Pointers
Pointers can be compared using the operators > and <.
SOURCES:
[1] Understanding and Using C Pointers by Richard Reese (O’Reilly). Copyright 2013 Richard Reese, Ph.D. 978-1-449-34418-4
[2] PRINZ, Peter a Ulla KIRCH-PRINZ. C Pocket Reference. O’REILLY, 2003. ISBN 978-0-596-00436-1.