zoukankan      html  css  js  c++  java
  • C语言结构体指针(指向结构体的指针)详解

    C语言结构体指针详解

    一.前言

    一个指向结构体的变量的指针表示的是这个结构体变量占内存中的起始位置,同样它也可以指向结构体变量数组。

    *a).b 等价于 a->b。

    "."一般情况下读作"的”,结构体a的b。

    “->”一般读作"指向的结构体的",a指向的结构体的b。

    二.实例

    #include<stdlib.h>
    #include<stdio.h>
    #include <string.h>
    
    int main(){
        struct{
            char *name;  //姓名
            int num;  //学号
            int age;  //年龄
            char group;  //所在小组
            float score;  //成绩
        } stu1 = { "Tom", 12, 18, 'A', 136.5 }, *pstu = &stu1;
        //读取结构体成员的值
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!
    ", (*pstu).name, (*pstu).num, (*pstu).age, (*pstu).group, (*pstu).score);
        printf("%s的学号是%d,年龄是%d,在%c组,今年的成绩是%.1f!
    ", pstu->name, pstu->num, pstu->age, pstu->group, pstu->score);
        return 0;
    }

    三.typedef struct与struct定义结构体的区别

    typedef是类型定义的意思。typedef struct 是为了使用这个结构体方便。
    具体区别在于:
    (1)若struct node {}这样来定义结构体的话。在申请node 的变量时,需要这样写,struct node n;
    (2)若用typedef,可以这样写,typedef struct node{}NODE; 。在申请变量时就可以这样写,NODE n;
    区别就在于使用时,是否可以省去struct这个关键字。

    struct 结构名
    {
    类型 变量名;
    类型 变量名;
    ...
    } 结构变量;

    typedef struct 结构名
    {
    类型 变量名;
    类型 变量名;
    ...
    } 结构别名;

     
  • 相关阅读:
    python_函数_文件
    Day_2_Python_str_list_dict的使用
    Day_1_Python_循环和格式化
    influxdb2.0版本部署+自启
    格式化Java内存工具JOL输出
    卷心菜的屯币日记
    influxDB时序数据库2.0FLUX查询语法使用记录
    两种转换2021-01-01T00:00:00Z为2021-01-01 00:00:00时间格式的方式(UTC时间转为yyyy-MM-dd HH:mm:ss)
    ThreadLocal的用处
    CentOS7使用ISO镜像文件作为离线Yum源
  • 原文地址:https://www.cnblogs.com/shierlou-123/p/12343114.html
Copyright © 2011-2022 走看看