Cprogramming 简明教程

Dereference Pointer in C

Dereference Pointer in C

dereference operator 用于访问并操纵 pointer 指向的 variable 中存储的值。 dereferenceindirection operator ( * ) 充当一元运算符,并且需要一个指针变量作为其操作数。

The dereference operator is used to access and manipulate the value stored in the variable pointed by the pointer. The dereference or indirection operator (*) acts as a unary operator, and it needs a pointer variable as its operand.

Syntax

以下是解引用指针的语法 −

Below is the syntax to dereference a pointer −

*pointer_variable;

通过上述语法(解引用指针),可以获取并更新指针指向的任何变量的值。

With the help of the above syntax (dereference pointer), you can get and update the value of any variable that is pointing by the pointer.

How to Dereference a Pointer?

若要解引用指针,需要遵循以下给定的步骤:

To dereference a pointer, you need to follow the below-given steps:

  1. Create a variable and declare a pointer variable.

  2. Initialize the pointer by assigning the address of the variable.

  3. Now, you can dereference the pointer to get or update the value of the variable.

Example

在这个例子中,我们演示了解引用指针的这三个步骤 −

In this example, we are demonstrating these three steps to deference a pointer −

#include <stdio.h>

int main() {
  // Create a variable and pointer variable
  int x = 10;
  int *ptr;

  // Initialize the pointer by assigning
  // the address of the variable
  ptr = &x;

  // Dereference the pointer
  printf("Value of x = %d\n", *ptr);

  return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Value of x = 10

What is Dereferencing?

“解引用”一词指的是访问指针所引用的内存地址中存储的值。解引用运算符(也称为 indirection operator )获取目标变量的值。

The term "dereferencing" refers to accessing the value that is stored in the memory address referred by the pointer. The dereference operator (also called indirection operator) fetches the value of the target variable.

Example

在上面的示例中,如果我们打印“ b ”,你将获得“ a ”的值,即 10。类似地,打印“ y ”显示 10.5。

In the above example, if we print "b", you get the value of "a", i.e., 10. Similarly, printing "y" displays 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\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Address of 'a': 6422028 Value of 'a': 10
Address of 'x': 6422024 Value of 'x': 10.500000

Manipulating Value by Dereferencing Pointer

解引用运算符还有助于间接操作指针所引用的变量的值。

The dereference operator also helps in indirectly manipulating the value of a variable referred to by a pointer.

Example

在这个示例中,我们在解引用指针的帮助下更改“a”和“x”的值 −

In this example, we change the value of "a" and "x" with the help of the dereference pointer −

#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\n", b, *b);
   printf ("Address of 'x': %d Value of 'x': %f\n", y, *y);

   return 0;
}

运行代码并检查其输出:

Run the code and check its output −

Address of 'a': 6422028 Value of 'a': 100
Address of 'x': 6422024 Value of 'x': 100.500000

Dereferencing a Double Pointer

就像你将普通变量的地址存储在它的指针中一样,你也可以拥有一个指针,它存储另一个指针的地址。具有另一个指针地址的指针称为 double pointer 或指针到指针。

Just as you store the address of a normal variable in its pointer, you can have a pointer that stores the address of another pointer as well. A pointer having address of another pointer is known as double pointer or pointer-to-pointer.

让我们声明一个指向整数类型的指针,并在其中存储整数变量的地址。

Let us declare a pointer to integer type and store the address of an integer variable in it.

int a = 10;
int *b = &a;

解引用运算符通过指针获取值 −

The dereference operator fetches the value via the pointer −

printf("a: %d \n Pointer to 'a' is 'b': %d \n Value at 'b': %d", a, b, *b);

将打印整数变量的值、它的地址以及解引用指针得到的值 −

The value of integer variable, its address, and the value obtained by the dereference pointer will be printed as −

a: 10
Pointer to 'a' is 'b': 6422036
Value at 'b': 10

现在让我们声明一个指针,该指针可以存储“b”的地址,“b”本身是一个指向整数类型(写为“int ". Let’s assume that the compiler also allocates it the address 3000. Hence, "c" is a pointer to a pointer to int and should be declared as "*int * *”)的指针。

Let us now declare a pointer that can store the address of "b", which itself is a pointer to the integer type written as "int ". Let’s assume that the compiler also allocates it the address 3000. Hence, "c" is a pointer to a pointer to int and should be declared as "*int **".

int **c = &b;
printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);

你可以得到 b 的值(它是 a 的地址)、c 的值(它是 b 的地址)和 c 的解引用值(它是 a 的地址)

You get the value of b (which is the address of a), the value of c (which is the address of b), and the dereferenced value from c (which is the address of a)

b: 6422036
Pointer to 'b' is 'c': 6422024
Value at 'b': 6422036

由于“ c ”此处是一个双指针,其声明中的第一个星号指向“b”,而第二个星号又指向“ a ”。我们可以使用双重引用指针从“ c ”中获取“ a ”的值。

Since "c" is a double pointer here, the first asterisk in its declaration points to "b" and the second asterisk in turn points to "a". We can use the double reference pointer to obtain the value of "a" from "c".

printf("Value of 'a' from 'c': %d", **c);

这应该显示 a 的值为 10。

This should display the value of a as 10.

Example

试用下面给出的完整代码 −

Try out the complete code given below −

#include <stdio.h>

int main (){

   int a = 10;
   int *b = &a;
   printf("a: %d \n Address: %d \n Value at 'a': %d\n\n", a, b, *b);

   int **c = &b;
   printf("b: %d \n Pointer to 'b' is 'c': %d \n Value at 'b': %d\n", b, c, *c);
   printf("Value of 'a' from 'c': %d", **c);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

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

Dereferencing a Structure Pointer

关键字“struct”用于创建派生数据类型,其中包含一个或多个不同类型的元素。像普通变量一样,你可以声明一个 structure pointer 并存储它的地址。

The keyword "struct" is used to create a derived data type that consists of one or more elements of different types. Like a normal variable, you can declare a structure pointer and store its address.

struct book{
   char title[10];
   double price;
   int pages;
};

struct book b1 = {"Learn C", 650.50, 325};
struct book *ptr = &b1;

在 C 中,由箭头符号(→)表示的间接运算符用于获取由结构指针引用的结构变量的元素的值。

In C, the indirection operator represented by the arrow symbol (→) is used to obtain the values of the elements of the struct variable referred to by the struct pointer.

Example

ptr → title ”返回了 title 元素的值,这是“ b1.title ”返回的相同值。“ptr → price”等同于“b1.price”,等等。

"ptr → title" returns the value of the title element, the same value returned by "b1.title". "ptr → price" is equivalent to "b1.price", etc.

#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: \n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n\n", ptr->title, ptr->price, ptr->pages);

   printf("With . Operator:\n");
   printf("Title: %s \nPrice: %7.2lf \nNumber of Pages: %d\n", b1.title, b1.price, b1.pages);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

With -> Operator:
Title: Learn C
Price:  650.50
Number of Pages: 325

With . Operator:
Title: Learn C
Price:  650.50
Number of Pages: 325

Dereferencing a Nested Structure Pointer

尽管 C 使用箭头运算符(→)访问 structure 变量的元素,但不能用它来访问任何内部结构的元素。

Even though C uses the arrow operator (→) to access the elements of a structure variable, the elements of any internal struct cannot be accessed with it.

只有外部结构的元素可以使用 → 运算符访问。对于后续的内部结构元素,我们需要使用点 (.) 运算符。

Only the elements of the outer struct are accessible with the → operator. For subsequent inner struct elements, we need to use the dot (.) operator.

Example

以下示例显示了如何取消引用嵌套结构体指针:

The following example shows how you can dereference a nested struct pointer −

#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\n", ptr->name);
   printf("Salary: %f\n", ptr->salary);
   printf("Date of Birth: %d-%d-%d\n", ptr->d1.d, ptr->d1.m, ptr->d1.y);

   return 0;
}

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

Name: Arjun
Salary: 45000.000000
Date of Birth: 12-5-1990