🔗 Pointers & Memory指针与内存管理

Deep dive into pointers and memory operations in C深入理解 C 语言指针概念与内存操作

What is a Pointer?什么是指针?

A pointer stores a memory address rather than a value. With pointers, you can manipulate memory directly to implement efficient memory management and complex data structures. 指针存储的是内存地址而不是直接的值。通过指针,我们可以直接操作内存,实现高效的内存管理与复杂的数据结构。

内存可视化:
内存地址 0x1000: 10 (变量 int a = 10)
内存地址 0x1004: 0x1000 (指针 int *ptr = &a)
指针ptr存储的是变量a的内存地址
int a = 10; // 普通变量 int *ptr = &a; // 指针变量,存储a的地址 int value = *ptr; // 解引用,获取地址上的值

Core Pointer Operations指针的核心操作

1. 声明指针

int *ptr1; // 指向整数的指针 char *ptr2; // 指向字符的指针 float *ptr3; // 指向浮点数的指针

2. 获取地址 (&)

int x = 42; int *ptr = &x; // &x 获取变量x的内存地址

3. 解引用 (*)

int x = 42; int *ptr = &x; printf("%d\n", *ptr); // 输出: 42 *ptr = 100; // 修改x的值为100

💡 记忆技巧

  • &: address-of operator&: 取地址运算符
  • *: dereference operator*: 解引用运算符
  • ptr: the pointer itself (stores an address)ptr: 指针本身(存储地址)
  • *ptr: the value pointed to*ptr: 指针指向的值

Pointers as Function Parameters指针作为函数参数

Passing pointers enables call-by-reference semantics so functions can modify the caller’s variables. 通过指针作为函数参数,我们可以实现引用传递,让函数能够修改调用者的变量。

// 错误的方式:值传递,无法修改原变量 void swap_wrong(int a, int b) { int temp = a; a = b; b = temp; } // 正确的方式:指针传递,可以修改原变量 void swap_correct(int *a, int *b) { int temp = *a; // 解引用获取值 *a = *b; // 修改a指向的值 *b = temp; // 修改b指向的值 }

🎯 RGB Color Rotation ExampleRGB颜色转换实例

Let’s implement an RGB color rotation function — a classic Week 1 exercise:让我们实现一个RGB颜色轮换函数,这是Week 1的经典练习:

void change_colour(int *red, int *green, int *blue) { int temp = *red; // 保存red的值 *red = *blue; // new red = old blue *blue = *green; // new blue = old green *green = temp; // new green = old red }

Pointers and Arrays指针与数组

An array name is effectively a pointer to its first element. 数组名本质上就是指向数组第一个元素的指针。

int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr; // 指向数组第一个元素 printf("%d\n", *ptr); // 输出: 10 printf("%d\n", *(ptr + 1)); // 输出: 20 printf("%d\n", *(ptr + 2)); // 输出: 30

⚠️ 常见错误

  • Wild pointer: uninitialized pointer pointing at random addresses野指针: 未初始化的指针,指向随机地址
  • Null dereference: dereferencing NULL crashes the program空指针解引用: 对NULL指针进行解引用会导致程序崩溃
  • Out-of-bounds: accessing memory beyond allocation指针越界: 访问超出分配内存范围的地址

🎯 Practice & Consolidation练习与巩固

练习1: 指针基础

What is the output of the following code?以下代码的输出是什么?

int x = 10; int y = 20; int *ptr1 = &x; int *ptr2 = &y; *ptr1 = *ptr2; ptr2 = ptr1; *ptr2 = 30; printf("x = %d, y = %d\n", x, y);

Answer:答案: x = 30, y = 20x = 30, y = 20

Explanation:解析:

  • *ptr1 = *ptr2 makes x = 20*ptr1 = *ptr2 将x的值改为20
  • ptr2 = ptr1 makes ptr2 point to xptr2 = ptr1 让ptr2指向x
  • *ptr2 = 30 updates x to 30*ptr2 = 30 将x的值改为30
  • y remains 20 throughouty的值始终保持20

练习2: RGB颜色转换

Implement a function to convert RGB values to the CMYK color model:实现一个函数,将RGB值转换为CMYK颜色模式:

void rgb_to_cmyk(int r, int g, int b, float *c, float *m, float *y, float *k) { // 在这里实现CMYK转换逻辑 // 公式: C = 1 - R/255, M = 1 - G/255, Y = 1 - B/255 // K = min(C, M, Y) }

Hint:提示:

  • Normalize RGB to 0–1先将RGB值归一化到0-1范围
  • Compute CMY: C=1-R/255, M=1-G/255, Y=1-B/255计算CMY值:C = 1 - R/255, M = 1 - G/255, Y = 1 - B/255
  • Compute K: K = min(C, M, Y)计算K值:K = min(C, M, Y)
  • Adjust CMY: C=(C-K)/(1-K), similarly for M,Y调整CMY值:C = (C - K) / (1 - K), 同理调整M和Y
  • Return results via pointer parameters通过指针参数返回计算结果

Summary总结

Pointers are central to C and essential for systems programming. In this module, you learned:指针是C语言的核心概念,掌握指针对于系统编程至关重要。本周我们学习了:

🚀 下一步学习

After mastering pointer basics, next we’ll learn structs and how pointers operate on more complex data structures.掌握了指针基础后,我们将学习结构体,看看如何用指针操作更复杂的数据结构。