Birth: 2025-02-17 14:30:28
Modify:
Pointer
*
declare a pointer variable
int a = 5;
int* ptr = &a;
dereference a pointer
int a2 = *ptr; // 5
&
obtain address
int* ptr = &a;
double pointer
int a = 5; // just-a-value
int* ptr = &a; // value a's address
int** ptr2 = &ptr; // ptr pointer's address
Pointer as Method Parameters
* (Pointer)
void a(int* ptr) {
*ptr = 10; //dereference, modify value
}
int r = 1;
a(&r);
** (Pointer of Pointer)
void a(int** ptr) {
*ptr = new int(20); //deference, modify pointer
}
int* r = NULL;
a(&r);
*& (Reference to Pointer)
void a(int*& ptr) {
ptr = new int(20); //modify pointer without dereference it
}
int* r = NULL;
a(r);
Struct
Node* n = new Node;
Node* n = malloc(sizeof(Node));
allocated memory in heap space.
created a pointer n pointed to the allocated space.
the object must be deleted.
Node n;
Node n = {a,b,c...};
allocated memory in stack space.
will be deleted out of the scope.
Constructor
struct Node {
int value;
Node* next;
Node(int v): value(v) { // assign v to value
//constructor
next = NULL;
}
}