c语言指向结构体的指针_指向C中结构数组的指针

本文介绍了如何在C语言中高效使用结构体数组,包括通过结构体指针访问数组成员的方法,使用点操作符与箭头操作符来操作结构体的属性。
摘要由CSDN通过智能技术生成

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;
}
Pointer to Structure array in C

使用指针访问结构成员 (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语言指向结构体的指针

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值