c语言指向结构体的指针
Like we have array of integers, array of pointers etc, we can also have array of structure variables. And to use the array of structure variables efficiently, we use pointers of structure type. We can also have pointer to a single structure variable, but it is mostly used when we are dealing with array of structure variables.
就像我们有整数数组,指针数组等一样,我们也可以有结构变量数组。 为了有效地使用结构变量数组,我们使用结构类型的指针 。 我们也可以使用指向单个结构变量的指针,但是当我们处理结构变量数组时,通常使用它。
#include <stdio.h>
struct Book
{
char name[10];
int price;
}
int main()
{
struct Book a; //Single structure variable
struct Book* ptr; //Pointer of Structure type
ptr = &a;
struct Book b[10]; //Array of structure variables
struct Book* p; //Pointer of Structure type
p = &b;
return 0;
}
使用指针访问结构成员 (Accessing Structure Members with Pointer)
To access members of structure using the structure variable, we used the dot .
operator. But when we have a pointer of structure type, we use arrow ->
to access structure members.
要使用structure变量访问结构的成员,我们使用了dot .
操作员。 但是,当我们具有结构类型的指针时,我们使用arrow- ->
访问结构成员。
#include <stdio.h>
struct my_structure {
char name[20];
int number;
int rank;
};
int main()
{
struct my_structure variable = {"StudyTonight", 35, 1};
struct my_structure *ptr;
ptr = &variable;
printf("NAME: %s\n", ptr->name);
printf("NUMBER: %d\n", ptr->number);
printf("RANK: %d", ptr->rank);
return 0;
}
NAME: StudyTonight NUMBER: 35 RANK: 1
姓名:StudyTonight人数:35排名:1
翻译自: https://www.studytonight.com/c/pointers-to-structure-in-c.php
c语言指向结构体的指针