Essential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers

“From today onwards, study hard and make progress every day”

Repetition is the best method for memory; spend one minute each day to remember the basics of C language.

“C Language Beginner’s Essential Knowledge Points Note Series 100”

The following notes finally enter the practical series, which is also the most important and difficult part of C language. Keep it up, young ones!

36. A Comprehensive Summary of C Language Pointer Knowledge Points, All in One Article, Short but Full of Essence! Save It to Avoid Getting Lost!

1. Basic Concepts of Pointers

A pointer is a variable that stores a memory address; it is the essence of C language. There are many types of pointers, but the value of any type of pointer variable is essentially a memory address.

DataType *pointerVariableName;

Three Key Elements of Pointers

  1. 1. Directionality: Saves a certain memory address; the pointer variable itself does not store data, but the memory address it points to stores data.
  2. 2. Type Information: Determines the step size for pointer arithmetic (addition and subtraction).
  3. 3. Dereferencing: Accessing target data through a pointer; * is the dereference operator, and ultimately, the target data is accessed indirectly through the pointer variable.

2. Declaration and Initialization of Pointers

1. Basic Declaration Method

int *p;        // Pointer to int
char *cp;      // Pointer to char
double *dp;    // Pointer to double

2. Pointer Initialization

int x = 10;
int *p = &x;   // & is the address-of operator

float f = 3.14;
float *fp = &f;

3. Special Pointer Values

int *p1 = NULL;  // Null pointer (address 0)
int *p2;         // Uninitialized pointer (dangerous!)

3. Dereferencing Pointers

1. Accessing Data through Pointers

int x = 42;
int *p = &x;
printf("%d", *p);  // Outputs 42 (* is the dereference operator)

2. Modifying Data through Pointers

*p = 100;          // Equivalent to x = 100
printf("%d", x);   // Outputs 100

4. Pointer Arithmetic

1. Pointer Addition and Subtraction of Integers

int arr[5] = {10,20,30,40,50};
int *p = arr;      // Equivalent to &arr[0]

p++;               // Moves forward sizeof(int) bytes, points to arr[1]
printf("%d", *p);  // Outputs 20

2. Pointer Comparison

int *p1 = &arr[0];
int *p2 = &arr[4];
if (p1 < p2) {    // Compare memory addresses; memory addresses are unsigned integers, 32-bit machines are 32-bit unsigned integers, 64-bit machines are 64-bit unsigned integers
    printf("p1 is before p2");
}

5. Relationship between Pointers and Arrays

1. Array Name as Pointer Constant

int arr[3] = {1,2,3};
printf("%p %p", arr, &arr[0]);  // Outputs the same address

2. Pointer Traversal of Arrays

for (int *p = arr; p < arr+3; p++) {
    printf("%d ", *p);
}
// Outputs: 1 2 3

6. Concept of Multi-level Pointers

1. Double Pointer

int x = 10;
int *p = &x;
int **pp = &p;    // Pointer to a pointer

printf("%d", **pp);  // Outputs 10

2. Pointer Array

int a=1, b=2, c=3;
int *arr[3] = {&a, &b, &c};
printf("%d", *arr[1]);  // Outputs 2

7. Safety Issues with Pointers

1. Wild Pointer Issue

int *p;          // Uninitialized
*p = 10;         // Undefined behavior!

2. Pointer Out of Bounds

int arr[3];
int *p = arr;
*(p + 5) = 10;   // Out of bounds access!

3. Dereferencing Null Pointer

int *p = NULL;
*p = 5;          // Program crashes!

8. const and Pointers

1. Pointer to Constant

const int *p;    // Cannot modify target through p
int const *p;    // Equivalent notation

2. Pointer Constant

int *const p = &x;  // p cannot point to another address

3. Double const

const int *const p = &x;  // Neither can be modified

9. Typical Applications of Pointers

1. Function Parameter Passing, Achieving a Similar Effect to Reference Passing

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

Essentially, it is still value passing, but the pointer value is passed, which can achieve the effect of reference passing.

2. Dynamic Memory Allocation

int *arr = malloc(10 * sizeof(int));
if (arr) {
    arr[0] = 100;  // Using dynamic memory through pointers
    // ......

    free(arr);
}

Some students contacted me, wanting to have a study exchange group. Previously, I was worried about advertisements, so I didn’t set up a group. Considering that having a group is indeed more convenient, I will try to set up a group this time.

If you need it, hurry up and join; the validity period is 7 days.

Essential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers

———- End ———-

[Special Statement: All articles in this public account are original or authorized by the author. Some content and images are sourced from the internet. Please feel free to consume them. The views are for learning reference only. If there are any errors or omissions, please forgive me.]

Essential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers

Essential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers

“If you like C, please like it”Essential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers Click the bottom right corner to seeEssential Knowledge Points for C Language Beginners: A Comprehensive Summary of Pointers

Leave a Comment