Pointer operators
For all value and reference types, there's a pointer version of it(T*
), the pointer instance stores the address of the variable.
operator | name | usage |
---|---|---|
& | address-of operator | returns a pointer to the address of a variable |
* | dereference operator | returns the variable at the address of a pointer, in other words, returns a pointer |
-> | pointer-to-member operator | syntactic sugar in which x->y is equivalent to (*x).y |
++ | pointer addition operator | adds sizeof(T) to the address contained in the variable |
-- | pointer subtraction operator | subtracts sizeof(T) to the address contained in the variable |
[] | pointer element accessor | retrieve value of address with an offset, in which p[i] is equivalent to *(p + i) |
Pointer Increment and Decrement
If a pointer increment or decrement operation overflows the domain of the pointer type, the result is implementation-defined, but no exceptions are produced.
cs
T* operator ++(T* x);
T* operator --(T* x);
1
2
2
Pointer Arithmetic
cs
T* operator +(T* x, int y);
T* operator +(T* x, uint y);
T* operator +(T* x, long y);
T* operator +(T* x, ulong y);
T* operator +(int x, T* y);
T* operator +(uint x, T* y);
T* operator +(long x, T* y);
T* operator +(ulong x, T* y);
T* operator –(T* x, int y);
T* operator –(T* x, uint y);
T* operator –(T* x, long y);
T* operator –(T* x, ulong y);
long operator –(T* x, T* y);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
Pointer comparison
See Void Pointer
Operator priority
++
or--
>*
,*p++
is equivalent to*(p++)
.