Basic Pointer
A pointer is a variable declared to store a memory address. Its declaration will also tell you the type of data it points to. You can retrieve the memory address of your variables, then mess with them.
To declare a pointer you can use int *pt
or int* pt
, note that this two syntax will do the same thing — make a pointer pointing to type int
. However, I personally prefer the first one since it indicates that pt is a pointer, pointing to int
, the second one indicates that pt is an int pointer, which is certainly not the case.
The type int
just tell the compiler how to read the data, for example, if you declare a variable with type short
(which is 2 bytes), they just store binary data into memory and read exactly 2 bytes, we can typecast it to int
, then we read 4 bytes next time(may lead to memory corruption though). Type is just ways to read data!
As a result, every pointer is 8 bytes(in 64 bits) since they refer to memory location, can read it with the type you specify. For example, int *pt
means that pt
is a location, read that location as int
.
Also, int* pt, not_a_pointer;
is a common pitfall. Then you can see why I prefer the first declaration!
Now we know that a pointer is nothing special but a memory location, but the location itself is not enough.
To get the value inside the location, we need *
again, however, this time we are not declaring pointers, we dereference it. This means, we set/get value of it.
Get the memory location of a value is called reference, get the value of the location is called dereference.
Pitfall comes again! We need to first make sure that it is pointing to a valid address!
In the example above, we use &
to get the address of a variable, this is called reference a variable.
NULL
(in C) or nullptr
(in C++) is a safe way to initialize a pointer, the former is a int
, with value set to 0, the second is a pointer
points to nothing
More Than Basic
- Pointer arithmetic (If you were to design this, what would you do? Think about it!)
pt++
vs*pt++
- Arrays are just pointer (since they are memory)
- Pointer to pointer (And more, aka 2d-array)
malloc
andfree
,new
anddelete
,new []
anddelete[]
(Best and basic practice)- Memory is the key!
- Stack and heap. See also Memory and malloc
const
pointer. See also: C++ Const- Complex declaration: cdecl: C gibberish ↔ English
- C++ Smart Pointer
2022-07-28 Update
其实要分辨指针,要(1)分清楚星号所在的地方,是个左值(left value),还是个右值(right value),(2)分清楚指针所指的类型,加上一些内存的通识就能理解了。
General Pointer
*
在 int 后还是数值前
As far as C goes they both do the same thing. It is a matter of preference.
int* i
shows clearly that it is an int pointer type.int *i
shows the fact that the asterisk only affects a single variable. Soint *i, j
andint* i, j
would both createi
as an int pointer andj
as an int.
int *i, j
和 int* i, j
效果都一样,一个指针和一个整数
- 两指针可以相减,不可相加。若要进行相减运算,则两指针必须指向同一数组,相减结果为相距的数组元素个数
Pointer to Function
URL: How do function pointers in C work? - Stack Overflow
- Use Case
- Via typedef