C 语言中的解引用指针
解引用运算符用于访问和操作由指针指向的变量中存储的值。解引用或间接运算符 (*) 充当一元运算符,它需要一个指针变量作为其操作数。
语法
以下是解引用指针的语法 -
*pointer_variable;
借助上述语法(解引用指针),您可以获取和更新指针指向的任何变量的值。
如何解引用指针?
要解引用指针,您需要遵循以下步骤:
- 创建一个变量并声明一个指针变量。
- 通过分配变量的地址来初始化指针。
- 现在,您可以解引用指针来获取或更新变量的值。
示例
在此示例中,我们将演示解引用指针的三个步骤 -
#include <stdio.h>
int main() {
// 创建一个变量和一个指针变量
int x = 10;
int *ptr;
// 通过赋值初始化指针
// 变量的地址
ptr = &x;
// 取消引用指针
printf("Value of x = %d
", *ptr);
return 0;
}
输出
运行代码并检查其输出 −
Value of x = 10
什么是解引用?
"解引用"是指访问指针指向的内存地址中存储的值。解引用运算符(也称为间接运算符)用于获取目标变量的值。
示例
在上面的例子中,如果我们打印"*b",则会得到"a"的值,即 10。同样,打印"*y"会显示 10.5。
#include <stdio.h>
int main (){
int a = 10;
int *b = &a;
float x = 10.5;
float *y = &x;
printf ("Address of 'a': %d Value of 'a': %d
", b, *b);
printf ("Address of 'x': %d Value of 'x': %f
", y, *y);
return 0;
}
输出
运行代码并检查其输出 −
Address of 'a': 6422028 Value of 'a': 10 Address of 'x': 6422024 Value of 'x': 10.500000
通过解引用指针操作值
解引用运算符也有助于间接操作指针引用的变量的值。
示例
在此示例中,我们借助解引用指针更改"a"和"x"的值 -
#include <stdio.h>
int main (){
int a = 10;
int *b = &a;
float x = 10.5;
float *y = &x;
*b = 100;
*y = 100.50;
printf ("Address of 'a': %d Value of 'a': %d
", b, *b);
printf ("Address of 'x': %d Value of 'x': %f
", y, *y);
return 0;
}
输出
运行代码并检查其输出 −
Address of 'a': 6422028 Value of 'a': 100 Address of 'x': 6422024 Value of 'x': 100.500000
解引用双指针
就像将普通变量的地址存储在其指针中一样,您也可以使用一个指针来存储另一个指针的地址。具有另一个指针地址的指针称为双指针或指向指针的指针。
我们声明一个指向整数类型的指针,并在其中存储一个整数变量的地址。
int a = 10; int *b = &a;
解引用运算符通过指针获取值 -
printf("a: %d
Pointer to 'a' is 'b': %d
Value at 'b': %d", a, b, *b);
整数变量的值、其地址以及取消引用指针获得的值将打印为 -
a: 10 Pointer to 'a' is 'b': 6422036 Value at 'b': 10
现在我们声明一个指针,用于存储"b"的地址,而"b"本身是一个指向整数类型的指针,写为"int *"。假设编译器也为其分配了地址 3000。因此,"c"是一个指向 int 类型的指针,应该声明为"int **"。
int **c = &b;
printf("b: %d
Pointer to 'b' is 'c': %d
Value at 'b': %d
", b, c, *c);
您将获得 b 的值(即 a 的地址)、c 的值(即 b 的地址)以及从 c 解引用的值(即 a 的地址)
b: 6422036 Pointer to 'b' is 'c': 6422024 Value at 'b': 6422036
由于"c"在此处是双指针,因此其声明中的第一个星号指向"b",而第二个星号又指向"a"。我们可以使用双引用指针从"c"获取"a"的值。
printf("Value of 'a' from 'c': %d", **c);
这应该显示 a 的值是 10。
示例
尝试下面给出的完整代码 -
#include <stdio.h>
int main (){
int a = 10;
int *b = &a;
printf("a: %d
Address: %d
Value at 'a': %d
", a, b, *b);
int **c = &b;
printf("b: %d
Pointer to 'b' is 'c': %d
Value at 'b': %d
", b, c, *c);
printf("Value of 'a' from 'c': %d", **c);
return 0;
}
输出
运行此代码时,将产生以下输出 -
a: 10 Address: 6422036 Value at a: 10 b: 6422036 Pointer to 'b' is 'c': 6422024 Value at 'b': 6422036 Value of 'a' from 'c': 10
取消引用结构体指针
关键字"struct"用于创建由一个或多个不同类型的元素组成的派生数据类型。与普通变量一样,您可以声明一个结构体指针并存储其地址。
struct book{
char title[10];
double price;
int pages;
};
struct book b1 = {"Learn C", 650.50, 325};
struct book *ptr = &b1;
在 C 语言中,箭头符号 () 表示的间接运算符用于获取结构体指针所引用结构体变量元素的值。
示例
"ptr -> title"返回 title 元素的值,与"b1.title"返回的值相同。"ptr -> price"等同于"b1.price",等等。
#include <stdio.h>
struct book{
char title[10];
double price;
int pages;
};
int main (){
struct book b1 = {"Learn C", 650.50, 325};
struct book *ptr = &b1;
printf("With -> Operator:
");
printf("Title: %s
Price: %7.2lf
Number of Pages: %d
", ptr->title, ptr->price, ptr->pages);
printf("With . Operator:
");
printf("Title: %s
Price: %7.2lf
Number of Pages: %d
", b1.title, b1.price, b1.pages);
return 0;
}
输出
运行此代码时,将产生以下输出 -
With -> Operator: Title: Learn C Price: 650.50 Number of Pages: 325 With . Operator: Title: Learn C Price: 650.50 Number of Pages: 325
取消引用嵌套结构体指针
尽管 C 语言使用箭头运算符 () 访问结构体变量的元素,但任何内部结构体的元素都无法通过该运算符访问。
只有外部结构体的元素可以使用该运算符访问。对于后续的内部结构体元素,我们需要使用点 (.) 运算符。
示例
以下示例展示了如何取消引用嵌套结构体指针 -
#include <stdio.h>
#include <string.h>
struct employee{
char name[10];
float salary;
struct dob {
int d, m, y;
} d1;
};
int main(){
struct employee e1 = {"Arjun", 45000, {12, 5, 1990}};
struct employee *ptr = &e1;
printf("Name: %s
", ptr->name);
printf("Salary: %f
", ptr->salary);
printf("Date of Birth: %d-%d-%d
", ptr->d1.d, ptr->d1.m, ptr->d1.y);
return 0;
}
输出
运行此代码时,将产生以下输出 -
Name: Arjun Salary: 45000.000000 Date of Birth: 12-5-1990

