<返回目录     Powered by claude/xia兄

第7课: 指针详解

指针基础

#include <iostream>
using namespace std;

int main() {
    int num = 10;
    int *ptr;  // 声明指针

    ptr = #  // 指针存储变量的地址

    cout << "num的值:" << num << endl;
    cout << "num的地址:" << &num << endl;
    cout << "ptr存储的地址:" << ptr << endl;
    cout << "ptr指向的值:" << *ptr << endl;  // 解引用

    // 通过指针修改值
    *ptr = 20;
    cout << "修改后num的值:" << num << endl;

    return 0;
}

空指针和野指针

#include <iostream>
using namespace std;

int main() {
    // 空指针:不指向任何地址
    int *ptr1 = nullptr;  // C++11推荐
    int *ptr2 = NULL;     // C风格
    int *ptr3 = 0;

    // 使用前检查
    if (ptr1 != nullptr) {
        cout << *ptr1 << endl;
    } else {
        cout << "ptr1是空指针" << endl;
    }

    // 野指针:指向未知地址(危险!)
    // int *ptr4;  // 未初始化
    // *ptr4 = 10;  // 错误!可能导致程序崩溃

    return 0;
}

指针与数组

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;  // 数组名就是首元素地址

    // 通过指针访问数组
    cout << "第一个元素:" << *ptr << endl;
    cout << "第二个元素:" << *(ptr + 1) << endl;

    // 指针遍历数组
    for (int i = 0; i < 5; i++) {
        cout << *(ptr + i) << " ";
    }
    cout << endl;

    // 等价写法
    for (int i = 0; i < 5; i++) {
        cout << ptr[i] << " ";
    }
    cout << endl;

    return 0;
}

指针运算

#include <iostream>
using namespace std;

int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int *ptr = arr;

    cout << "ptr指向:" << *ptr << endl;

    ptr++;  // 指针移动到下一个元素
    cout << "ptr++后指向:" << *ptr << endl;

    ptr += 2;  // 指针向后移动2个位置
    cout << "ptr+=2后指向:" << *ptr << endl;

    ptr--;  // 指针向前移动
    cout << "ptr--后指向:" << *ptr << endl;

    // 指针相减
    int *start = arr;
    int *end = &arr[4];
    cout << "数组元素个数:" << (end - start + 1) << endl;

    return 0;
}

指针作为函数参数

#include <iostream>
using namespace std;

// 通过指针修改变量值
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

// 指针访问数组
void printArray(int *arr, int size) {
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    int x = 10, y = 20;
    cout << "交换前:x=" << x << ", y=" << y << endl;

    swap(&x, &y);
    cout << "交换后:x=" << x << ", y=" << y << endl;

    int arr[] = {1, 2, 3, 4, 5};
    printArray(arr, 5);

    return 0;
}

指向指针的指针

#include <iostream>
using namespace std;

int main() {
    int num = 100;
    int *ptr = #      // 一级指针
    int **pptr = &ptr;    // 二级指针

    cout << "num的值:" << num << endl;
    cout << "通过ptr访问:" << *ptr << endl;
    cout << "通过pptr访问:" << **pptr << endl;

    // 修改值
    **pptr = 200;
    cout << "修改后num的值:" << num << endl;

    return 0;
}

常量指针与指针常量

#include <iostream>
using namespace std;

int main() {
    int a = 10, b = 20;

    // 常量指针:指向的值不能改,指针可以改
    const int *ptr1 = &a;
    // *ptr1 = 20;  // 错误!
    ptr1 = &b;      // 正确

    // 指针常量:指针不能改,指向的值可以改
    int * const ptr2 = &a;
    *ptr2 = 30;     // 正确
    // ptr2 = &b;   // 错误!

    // 常量指针常量:都不能改
    const int * const ptr3 = &a;
    // *ptr3 = 40;  // 错误!
    // ptr3 = &b;   // 错误!

    return 0;
}

练习题

  1. 使用指针实现字符串长度计算函数
  2. 使用指针实现数组逆序
  3. 编写函数使用指针返回数组的最大值和最小值
  4. 使用指针实现字符串拷贝函数