结构体指针
1. 完整例子
| 1 | #include <stdio.h> |
| 2 | |
| 3 | struct Point { int x, y; }; |
| 4 | |
| 5 | void print_point(const struct Point *pt) { |
| 6 | if (!pt) return; |
| 7 | printf("(%d,%d)\n", pt->x, pt->y); |
| 8 | } |
| 9 | |
| 10 | int main(void) { |
| 11 | struct Point p = {1, 2}; |
| 12 | print_point(&p); |
| 13 | return 0; |
| 14 | } |
2. 为什么传指针
- 大结构少拷贝
- 函数需要修改外部结构体时必须传指针
- const 表示只读意图