Cprogramming 简明教程
Dot (.) Operator in C
Dot (.) Operator in C
dot (.) operator in C 语言也称为 “ direction selection member ”。它用于选择结构和联合的成员。 dot (.) operator 是一个二元运算符,需要两个运算数(结构或联合名称和成员名称),并且它具有最高 operator precedence 。
The dot (.) operator in C language is also known as "direction selection member". It is used to select members of structure and union. The dot (.) operator is a binary operator that requires two operands (structure or union name and member name) and it has the highest operator precedence.
当你想要访问和操作结构和联合的成员 ( variables ) 时, dot (.) operator 很有用。
The dot (.) operator is useful when you want to access and manipulate the members (variables) of structure and union.
Using Dot (.) Operator
点 (.) 运算符使用结构和联合变量名称选择 structure 和联合的成员。以下是使用点 (.) 运算符访问结构或联合的成员的语法 −
The dot (.) operator selects the members of the structure and union using the structure and union variable name. Here is the syntax of using the dot (.) operator to access members of a structure or union −
var.member;
此处, var 是某个结构或联合类型的一个变量, member 是在创建结构或联合时定义的一个元素。
Here, var is a variable of a certain struct or a union type, and member is one of the elements defined while creating the structure or union.
Example
使用以下语法,通过 struct 关键字定义一个新的派生数据类型 −
A new derived data type is defined with struct keyword using the following syntax −
struct newtype {
type elem1;
type elem2;
type elem3;
...
...
};
然后,你可以声明这种派生数据类型的变量如下 −
You can then declare a variable of this derived data type as −
struct newtype var;
访问某个成员,
To access a certain member,
var.elem1;
Dot Operator with Structure (struct)
如上所述,点 (.) 运算符用于访问和操作结构的成员。
As discussed above, the dot (.) operator is used to access and manipulate the members of a structure.
Example
让我们声明一个名为 book 的结构类型和一个结构变量。以下示例演示如何使用点运算符 (.) 访问 book 结构中的成员。
Let us declare a struct type named book and a struct variable. The following example shows how you can use the dot operator (.) to access the members in the book structure.
看一看这个示例 −
Take a look at the example −
#include <stdio.h>
struct book {
char title[10];
double price;
int pages;
};
int main (){
struct book b1 = {"Learn C", 675.50, 325};
printf("Title: %s\n", b1.title);
printf("Price: %lf\n", b1.price);
printf("No of Pages: %d\n", b1.pages);
printf("size of book struct: %d", sizeof(struct book));
return 0;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Title: Learn C
Price: 675.500000
No of Pages: 325
size of book struct: 32
Dot Operator with Union
C 中的 union 关键字还允许你定义一个派生数据类型,与 struct 关键字非常相似。但是,与结构变量不同,联合类型变量仅一个成员可以在任何给定时间包含一个值。
The union keyword in C also lets you define a derived data type, very much similar to the struct keyword. However, unlike a struct variable, a variable of union type, only one of its members can contain a value at any given time.
点 (.) 运算符还用于访问和操作 union 的成员。
The dot (.) operator is also used to access and manipulate the members of a union.
Example
你还可以使用点运算符访问联合成员元素,如本示例所示 −
You can also use the dot operator to access union member elements, as shown in this example −
#include <stdio.h>
union Data {
int i;
float f;
char str[20];
};
int main(){
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
编译并执行上述代码后,将产生以下结果 −
When the above code is compiled and executed, it produces the following result −
data.i : 1917853763
data.f : 4122360580327794860452759994368.000000
data.str : C Programming
Dot Operator with Nested Structure
Nested structures 在结构类型的某个元素本身是多个类型的一种复合表示形式时定义。
Nested structures are defined when one of the elements of a struct type is itself a composite representation of one or more types.
点运算符还可用于访问嵌套结构(以及联合类型)的成员。它可以与普通结构相同的方式进行。
The dot operator can also be used to access the members of nested structures (and union types also). It can be done in the same way as done for the normal structure.
假设我们有一个嵌套结构,如下所示 −
Suppose we have a nested structure as follows −
struct struct1 {
var1;
var2;
struct struct2 {
var3;
var4;
} s2;
} s1;
在这种情况下,s1 的成员以前的访问方式(例如 s1.var1 和 s1.var2),内部结构的成员访问方式如下 −
In this case, the members of s1 are accessed as previously (as s1.var1 and s1.var2), and the members of inner struct are accessed as −
s1.s2.var3;
Example
在本示例中,我们有一个 employee 数据类型,其中一个元素是出生日期 (dob)。我们将使用 employee 结构中的三个 int 类型 “d”、“m” 和 “y” 声明 dob 结构,并且其变量 d1 是外部类型的一个元素。
In this example, we have an employee data type with one of its elements being the date of birth (dob). We shall declare the dob struct with three int types "d", "m" and "y" inside the employee structure and its variable d1 is one of the elements of the outer type.
#include <stdio.h>
struct employee {
char name[10];
float salary;
struct dob {
int d, m, y;
} d1;
};
int main(){
struct employee e1 = {"Kiran", 25000, {12, 5, 1990}};
printf("Name: %s\n", e1.name);
printf("Salary: %f\n", e1.salary);
printf("Date of Birth: %d-%d-%d\n", e1.d1.d, e1.d1.m, e1.d1.y);
return 0;
}
运行代码并检查其输出:
Run the code and check its output −
Name: Kiran
Salary: 25000.000000
Date of Birth: 12-5-1990
Accessing the Members Using the Arrow Operator
C 还有另一种方法来访问结构变量的成员。它可以使用指向结构变量的 pointer ,通过箭头运算符 (→) 来完成。
C also has another method to access the members of a struct variable. It can be done with the arrow operator (→) with the help of a pointer to the struct variable.
使用 struct 关键字定义了新的派生数据类型,语法如下:
A new derived data type is defined with struct keyword as following syntax −
struct newtype {
type elem1;
type elem2;
type elem3;
...
...
};
然后,您可以声明该派生数据类型的变量及其指针:
You can then declare a variable of this derived data type, and its pointer as −
struct newtype var;
struct newtype *ptr=&var;
要通过指针访问某个成员,请使用以下语法:
To access a certain member through the pointer, use the syntax
ptr->elem1;
Example
请看以下示例:
Take a look at the following example −
#include <stdio.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;
}
当你运行这段代码时,它将产生以下输出:
When you run this code, it will produce the following output −
Title: Learn C
Price: 675.500000
No of Pages: 325
Accessing the Elements of a Nested Inner Structure
对于 nested structure ,
In case of a nested structure,
struct struct1 {
var1;
var2;
struct struct2 {
var3;
var4;
} s2;
} s1;
struct struct1 *ptr=&s1;
要访问嵌套结构的内部结构的元素,我们使用以下语法:
To access the elements of the inner structure of a nested structure, we use the following syntax −
ptr -> s2.var3;
Example
请看以下示例:
Take a look at the following example −
#include <stdio.h>
struct employee {
char name[10];
float salary;
struct dob {
int d, m, y;
} d1;
};
int main(){
struct employee e1 = {"Kiran", 25000, {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;
}
运行代码并检查其输出:
Run the code and check its output −
Name: Kiran
Salary: 25000.000000
Date of Birth: 12-5-1990