56-c语言const修饰结构体指针
在C语言中,
const修饰结构体指针时,其位置决定语义:const struct S *ptr(内容常量指针)表示指针指向的结构体内容不可修改但指针本身可变;struct S *const ptr(指针常量)表示指针本身不可修改但内容可变;const struct S *const ptr(双重常量)表示指针和内容均不可修改。正确使用能增强代码安全性,避免意外修改数据,尤其适用于函数参数设计以明确接口的读取权限。
在C语言中,const修饰结构体指针时,其位置不同会导致完全不同的语义。理解const在指针声明中的位置对正确使用结构体指针至关重要。
1. const struct S *ptr(指向常量结构体的指针)
- 语义:指针指向的内容(结构体数据)不可修改,但指针本身可以指向其他地址
- 特点:内容不可变,指针可变
struct Point {
int x;
int y;
};
void example_usage() {
struct Point p1 = {1, 2};
struct Point p2 = {3, 4};
// 指向常量结构体的指针
const struct Point *ptr1 = &p1;
// 以下操作错误:不能修改结构体内容
// ptr1->x = 10;
// 以下操作正确:可以改变指针指向
ptr1 = &p2;
}
2. struct S *const ptr(指向结构体的常量指针)
- 语义:指针本身不可修改(不能指向其他地址),但指向的结构体内容可以修改
- 特点:内容可变,指针不可变
struct Point {
int x;
int y;
};
void example_usage() {
struct Point p1 = {1, 2};
struct Point p2 = {3, 4};
// 指向结构体的常量指针
struct Point *const ptr2 = &p1;
// 以下操作正确:可以修改结构体内容
ptr2->x = 10;
// 以下操作错误:不能改变指针指向
// ptr2 = &p2;
}
3. const struct S *const ptr(指向常量结构体的常量指针)
- 语义:指针本身和指向的结构体内容均不可修改
- 特点:内容和指针均不可变
struct Point {
int x;
int y;
};
void example_usage() {
struct Point p1 = {1, 2};
// 指向常量结构体的常量指针
const struct Point *const ptr3 = &p1;
// 以下操作错误:不能修改结构体内容
// ptr3->x = 10;
// 以下操作错误:不能改变指针指向
// ptr3 = &p1;
}
实际应用场景
-
函数参数设计:当函数仅需读取结构体数据时,应使用
const struct S *作为参数void print_point(const struct Point *p) { printf("Point: (%d, %d)\n", p->x, p->y); // 不能修改p指向的内容 } -
接口安全设计:在需要确保指针和结构体内容都不被修改时使用
const struct S *constvoid process_point(const struct Point *const p) { // 既不能修改点坐标,也不能篡改指针本身 printf("Point: (%d, %d)\n", p->x, p->y); }
记忆技巧
- "左定值,右定向":
const在*左边修饰内容(定值),在*右边修饰指针(定向) - "就近原则":
const修饰其左侧最近的类型或标识符
注意事项
-
将非
const指针赋给const指针是允许的(添加const限定)struct Point p = {1, 2}; struct Point *p1 = &p; const struct Point *p2 = p1; // 正确 -
将
const指针赋给非const指针需要强制类型转换(不推荐)const struct Point p = {1, 2}; const struct Point *p1 = &p; struct Point *p2 = (struct Point *)p1; // 需要强制转换,但不推荐 // *p2 = {3, 4}; // 未定义行为,可能导致程序崩溃 -
避免返回局部结构体变量的地址,确保指针指向的有效性
正确使用const修饰结构体指针可以提高代码的安全性,防止意外修改数据,使接口设计更加清晰和健壮。
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
本文是原创文章,采用 CC BY-NC-ND 4.0 协议,完整转载请注明来自 软件从业者Hort
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果

