来个例子吧,指针的使用方法
时间: 2023-11-04 14:14:18 浏览: 38
下面是一个指针的使用方法的例子:
```c
#include <stdio.h>
int main() {
int num = 10; // 定义一个整型变量num,初始值为10
int *p; // 定义一个整型指针p
p = # // 将指针p指向num变量的地址
printf("num的值为:%d\n", num);
printf("num的地址为:%p\n", &num);
printf("p的值为:%p\n", p);
printf("p指向的值为:%d\n", *p); // 通过指针p访问num变量的值
*p = 20; // 通过指针p修改num变量的值
printf("num的值为:%d\n", num); // 输出修改后的num变量的值
return 0;
}
```
运行结果:
```
num的值为:10
num的地址为:0x7ffee6bfc7ac
p的值为:0x7ffee6bfc7ac
p指向的值为:10
num的值为:20
```
上述代码中,定义了一个整型变量`num`,并且定义了一个整型指针`p`。通过将指针`p`指向`num`变量的地址,访问`num`变量的值,修改`num`变量的值等操作,展示了指针的使用方法。
相关问题
函数指针使用例子
函数指针是一个指向函数的指针变量,可以在程序运行时动态地指向不同的函数。下面是一个使用函数指针的例子:
```c++
#include <iostream>
using namespace std;
// 声明一个函数指针类型
typedef int (*FuncPtr)(int, int);
// 定义两个函数
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
// 定义一个函数,该函数接受一个函数指针作为参数并调用它
void calculate(FuncPtr func, int a, int b) {
int result = func(a, b);
cout << "The result is: " << result << endl;
}
int main() {
int a = 5, b = 3;
calculate(add, a, b); // 调用 add 函数
calculate(subtract, a, b); // 调用 subtract 函数
return 0;
}
```
在上面的例子中,我们首先声明了一个函数指针类型 `FuncPtr`,它指向一个接受两个整型参数并返回一个整型结果的函数。然后,我们定义了两个函数 `add` 和 `subtract`,它们分别实现了加法和减法运算。接下来,我们定义了一个函数 `calculate`,它接受一个函数指针 `func`、两个整型参数 `a` 和 `b`,并调用指向的函数。最后,在主函数中,我们分别调用 `calculate` 函数并传递不同的函数指针作为参数。
运行上面的程序,输出如下:
```
The result is: 8
The result is: 2
```
可以看到,程序分别调用了 `add` 和 `subtract` 函数,并正确地计算出了结果。这就是一个简单的函数指针的使用例子。
结构体指针数组举个例子
结构体指针数组是指一个数组中的每个元素都是指向结构体的指针。通过使用结构体指针数组,可以方便地管理和操作多个结构体对象。
举个例子,假设我们有一个学生信息的结构体,包含学生的姓名和年龄:
```c
typedef struct {
char name[20];
int age;
} Student;
```
现在我们需要管理多个学生的信息,可以使用结构体指针数组来实现。首先定义一个结构体指针数组,并为每个学生分配内存空间:
```c
Student* students[3]; // 定义一个包含3个学生的结构体指针数组
students[0] = (Student*)malloc(sizeof(Student)); // 为第一个学生分配内存空间
strcpy(students[0]->name, "Alice"); // 设置第一个学生的姓名
students[0]->age = 18; // 设置第一个学生的年龄
students[1] = (Student*)malloc(sizeof(Student)); // 为第二个学生分配内存空间
strcpy(students[1]->name, "Bob"); // 设置第二个学生的姓名
students[1]->age = 19; // 设置第二个学生的年龄
students[2] = (Student*)malloc(sizeof(Student)); // 为第三个学生分配内存空间
strcpy(students[2]->name, "Charlie"); // 设置第三个学生的姓名
students[2]->age = 20; // 设置第三个学生的年龄
```
通过结构体指针数组,我们可以方便地访问和修改每个学生的信息。例如,可以打印出每个学生的姓名和年龄:
```c
for (int i = 0; i < 3; i++) {
printf("学生姓名:%s,年龄:%d\n", students[i]->name, students[i]->age);
}
```
这样就可以输出每个学生的信息了。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)