Cprogramming 简明教程
Pointers to Structures in C
如果你使用关键字 struct 定义了一个派生数据类型,那么就可以声明一个此类型的变量。因此,你还可以声明一个指针变量来存储其地址。因此,指向结构体的指针是一个指向结构体变量的变量。
If you have defined a derived data type using the keyword struct, then you can declare a variable of this type. Hence, you can also declare a pointer variable to store its address. A pointer to struct is thus a variable that refers to a struct variable.
Syntax: Defining and Declaring a Structure
以下是如何使用 “ struct ” 关键字定义一个新的派生数据类型 −
This is how you will define a new derived data type using the "struct" keyword −
struct type {
type var1;
type var2;
type var3;
...
...
};
然后,你可以按如下方式 declare 该派生数据类型的变量 −
You can then declare a variable of this derived data type as following −
struct type var;
然后,你可以声明一个指针变量,并存储 var 的地址。要将一个变量声明为指针,必须用 “ * ” 为其添加前缀;要获取变量的地址,我们使用 “ & ” 运算符。
You can then declare a pointer variable and store the address of var. To declare a variable as a pointer, it must be prefixed by "*"; and to obtain the address of a variable, we use the "&" operator.
struct type *ptr = &var;
Accessing the Elements of a Structure
To access the elements of a structure with pointer, we use a special operator called the *indirection operator (→) *.
此处,我们定义了一个名为 book 的用户定义的 struct 类型。我们声明一个 book 变量和一个指针。
Here, we define a user-defined struct type called book. We declare a book variable and a pointer.
struct book{
char title[10];
double price;
int pages;
};
struct book b1 = {"Learn C", 675.50, 325},
struct book *strptr;
要存储地址,请使用 & 运算符。
To store the address, use the & operator.
strptr = &b1;
Using the Indirection Operator
在 C 编程中,我们对结构体指针使用 indirection operator (“ → ”)。它也称为 “struct dereference operator”。它有助于访问指针引用的结构体变量的元素。
In C programming, we use the indirection operator ("→") with struct pointers. It is also called the "struct dereference operator". It helps to access the elements of a struct variable to which the pointer references to.
要访问结构体中的单个元素,请按如下方式使用 indirection operator −
To access an individual element in a struct, the indirection operator is used as follows −
strptr -> title;
strptr -> price;
strptr -> pages;
结构指针使用间接运算符或解引用运算符来取结构变量的结构元素的值。点运算符 (“ . ”) 用于取值来引用结构变量。因此,
The struct pointer uses the indirection operator or the dereference operator to fetch the values of the struct elements of a struct variable. The dot operator (".") is used to fetch the values with reference to the struct variable. Hence,
b1.title is the same as strpr -> title
b1.price is the same as strptr -> price
b1.pages is the same as strptr -> pages
Example: Pointers to Structures
下面的程序演示了对结构的指针的用法。在此示例中,“strptr” 是对变量 “struct book b1” 的指针。因此, “strrptr → title” 返回标题,与 “b1.title” 的作用类似。
The following program shows the usage of pointers to structures. In this example, "strptr" is a pointer to the variable "struct book b1". Hence, "strrptr → title" returns the title, similar to "b1.title" does.
#include <stdio.h>
#include <string.h>
struct book{
char title[10];
double price;
int pages;
};
int main(){
struct book b1 = {"Learn C", 675.50, 325};
struct book *strptr;
strptr = &b1;
printf("Title: %s\n", strptr -> title);
printf("Price: %lf\n", strptr -> price);
printf("No of Pages: %d\n", strptr -> pages);
return 0;
}
Title: Learn C
Price: 675.500000
No of Pages: 325
Points to Note
-
The dot operator (.) is used to access the struct elements via the struct variable.
-
To access the elements via its pointer, we must use the indirection operator (→).
Example
我们考虑另一个示例,以了解对结构的指针实际的工作原理。在这里,我们将使用关键字 struct 定义一个称为 person 的新的派生数据类型,然后我们声明一个其类型的变量和一个指针。
Let’s consider another example to understand how pointers to structures actually work. Here, we will use the keyword struct to define a new derived data type called person and then we will declare a variable of its type and a pointer.
要求用户输入姓名、年龄和体重。值通过使用间接运算符访问存储在结构元素中。
The user is asked to input the name, age and weight of the person. The values are stored in the structure elements by accessing them with the indirection operator.
#include <stdio.h>
#include <string.h>
struct person{
char *name;
int age;
float weight;
};
int main(){
struct person *personPtr, person1;
strcpy(person1.name, "Meena");
person1.age = 40;
person1.weight = 60;
personPtr = &person1;
printf("Displaying the Data: \n");
printf("Name: %s\n", personPtr -> name);
printf("Age: %d\n", personPtr -> age);
printf("Weight: %f", personPtr -> weight);
return 0;
}
当运行此程序时,它将生成以下输出 −
When you runt this program, it will produce the following output −
Displaying the Data:
Name: Meena
Age: 40
weight: 60.000000
C 允许您声明“结构数组”以及“指针数组”。在此,结构指针数组中的每个元素是对结构变量的引用。
C allows you to declare an "array of struct" as well as an "array of pointers". Here, each element in the struct pointer array is a reference to a struct variable.
结构变量类似于主类型的一个普通变量,您可以有结构数组,可以将结构变量传递给一个函数,以及从一个函数返回一个结构。
A struct variable is like a normal variable of primary type, in the sense that you can have an array of struct, you can pass the struct variable to a function, as well as return a struct from a function.
Note : 您需要在声明时为变量或指针的名称添加 “ struct type ” 的前缀。但是,您可以通过使用 typedef 关键字创建简写来避免它。
Note: You need to prefix "struct type" to the name of the variable or pointer at the time of declaration. However, you can avoid it by creating a shorthand notation using the typedef keyword.
Why Do We Need Pointers to Structures?
对结构的指针非常重要,因为您可以使用它们来创建复杂和动态的数据结构,例如链表、树、图等。此类数据结构使用 self-referential structs ,在其中我们定义一个结构类型,其中一个元素为指向同一类型的一个指针。
Pointers to structures are very important because you can use them to create complex and dynamic data structures such as linked lists, trees, graphs, etc. Such data structures use self-referential structs, where we define a struct type having one of its elements as a pointer to the same type.
对结构自身的一个元素的指针自引用结构的示例定义如下 −
An example of a self-referential structure with a pointer to an element of its own type is defined as follows −
struct mystruct{
int a;
struct mystruct *b;
};
我们将在下一章学习有关自引用结构的内容。
We shall learn about self-referential structures in the next chapter.