函数与指针¶
📖 章节简介¶
本章将介绍C++的函数和指针,包括函数定义、指针、引用和函数指针的使用。
上图展示了指针保存地址、解引用修改变量,以及指针参数传递对实参产生影响的核心机制。
🎯 函数基础¶
1. 函数定义¶
C++
// 函数定义
#include <iostream>
using namespace std;
// 函数声明
int add(int a, int b);
double calculateArea(double radius);
void printMessage(const string& message);
// 函数定义
int add(int a, int b) {
return a + b;
}
double calculateArea(double radius) {
return 3.141592653589793 * radius * radius;
}
void printMessage(const string& message) {
cout << message << endl;
}
int main() {
// 调用函数
int sum = add(10, 20);
cout << "10 + 20 = " << sum << endl;
double area = calculateArea(5.0);
cout << "半径为5的圆面积: " << area << endl;
printMessage("Hello, World!");
return 0;
}
2. 函数参数¶
C++
// 函数参数
#include <iostream>
using namespace std;
// 值传递
void valuePass(int x) {
x = 100;
cout << "函数内x = " << x << endl;
}
// 引用传递
void referencePass(int& x) {
x = 100;
cout << "函数内x = " << x << endl;
}
// 指针传递
void pointerPass(int* x) {
*x = 100;
cout << "函数内x = " << *x << endl;
}
// 默认参数
void greet(string name = "Guest") {
cout << "Hello, " << name << "!" << endl;
}
int main() {
int num = 10;
// 值传递
cout << "值传递前num = " << num << endl;
valuePass(num);
cout << "值传递后num = " << num << endl;
// 引用传递
num = 10;
cout << "\n引用传递前num = " << num << endl;
referencePass(num);
cout << "引用传递后num = " << num << endl;
// 指针传递
num = 10;
cout << "\n指针传递前num = " << num << endl;
pointerPass(&num);
cout << "指针传递后num = " << num << endl;
// 默认参数
cout << "\n默认参数:" << endl;
greet();
greet("张三");
return 0;
}
📍 指针基础¶
1. 指针定义¶
C++
// 指针定义
#include <iostream>
using namespace std;
int main() {
// 基本变量
int num = 10;
// 指针声明和初始化
int* ptr = #
// 输出
cout << "num的值: " << num << endl;
cout << "num的地址: " << &num << endl;
cout << "ptr的值: " << ptr << endl;
cout << "ptr指向的值: " << *ptr << endl;
// 通过指针修改值
*ptr = 20;
cout << "\n通过指针修改后num = " << num << endl;
// 指针运算
int arr[] = {10, 20, 30, 40, 50};
int* arrPtr = arr;
cout << "\n数组元素:" << endl;
for (int i = 0; i < 5; i++) {
cout << *(arrPtr + i) << " ";
}
cout << endl;
return 0;
}
2. 指针与数组¶
C++
// 指针与数组
#include <iostream>
using namespace std;
int main() {
// 数组
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
// 指针遍历数组
cout << "指针遍历数组:" << endl;
int* ptr = arr;
for (int i = 0; i < size; i++) {
cout << *(ptr + i) << " ";
}
cout << endl;
// 数组名作为指针
cout << "\n数组名作为指针:" << endl;
for (int i = 0; i < size; i++) {
cout << *(arr + i) << " ";
}
cout << endl;
// 指针运算
cout << "\n指针运算:" << endl;
ptr = arr;
ptr++; // 指向第二个元素
cout << "ptr++后: " << *ptr << endl;
ptr += 2; // 指向第四个元素
cout << "ptr += 2后: " << *ptr << endl;
return 0;
}
🔄 引用¶
1. 引用基础¶
C++
// 引用基础
#include <iostream>
using namespace std;
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
// 引用声明
int num = 10;
int& ref = num;
// 输出
cout << "num的值: " << num << endl;
cout << "ref的值: " << ref << endl;
cout << "num的地址: " << &num << endl;
cout << "ref的地址: " << &ref << endl;
// 通过引用修改值
ref = 20;
cout << "\n通过引用修改后num = " << num << endl;
// 使用引用交换值
int x = 10, y = 20;
cout << "\n交换前x = " << x << ", y = " << y << endl;
swap(x, y);
cout << "交换后x = " << x << ", y = " << y << endl;
return 0;
}
🎯 函数指针¶
1. 函数指针基础¶
C++
// 函数指针
#include <iostream>
using namespace std;
// 函数声明
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
// 函数定义
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int main() {
// 函数指针声明
int (*funcPtr)(int, int);
// 指向add函数
funcPtr = add;
cout << "10 + 20 = " << funcPtr(10, 20) << endl;
// 指向subtract函数
funcPtr = subtract;
cout << "10 - 20 = " << funcPtr(10, 20) << endl;
// 指向multiply函数
funcPtr = multiply;
cout << "10 * 20 = " << funcPtr(10, 20) << endl;
// 函数指针数组
int (*funcArray[3])(int, int) = {add, subtract, multiply};
cout << "\n函数指针数组:" << endl;
for (int i = 0; i < 3; i++) {
cout << "funcArray[" << i << "](10, 20) = " << funcArray[i](10, 20) << endl;
}
return 0;
}
💡 最佳实践¶
1. 指针安全¶
C++
// 指针安全
#include <memory>
int main() {
// ✅ 好的做法:初始化指针
int num = 10;
int* ptr = #
// ❌ 不好的做法:未初始化的指针
int* unsafePtr;
// *unsafePtr = 20; // 危险!
// ✅ 好的做法:检查指针是否为空
if (ptr != nullptr) {
*ptr = 20;
}
// ✅ 好的做法:使用智能指针
std::unique_ptr<int> smartPtr = std::make_unique<int>(10);
return 0;
}
2. 函数设计¶
C++
// 函数设计
#include <iostream>
#include <string>
using namespace std;
// ✅ 好的做法:函数有明确的返回类型
int calculate(int a, int b) {
return a + b;
}
// ❌ 不好的做法:函数返回void但需要返回值
void calculateBad(int a, int b, int& result) {
result = a + b;
}
// ✅ 好的做法:使用const修饰符
void printString(const string& str) {
cout << str << endl;
}
// ❌ 不好的做法:不使用const
void printStringBad(string& str) {
cout << str << endl;
}
int main() {
cout << calculate(10, 20) << endl;
return 0;
}
📝 练习题¶
基础题¶
- 如何定义和调用函数?
- 指针和引用有什么区别?
- 什么是函数指针?
进阶题¶
- 使用指针操作数组。
- 实现函数指针数组。
- 使用引用实现值交换。
实践题¶
- 编写一个数学函数库。
- 实现一个简单的排序函数。
- 创建一个字符串处理工具。
📚 推荐阅读¶
🔗 下一章¶
类与对象 - 学习C++的面向对象编程。