zoukankan      html  css  js  c++  java
  • 结构体

    结构体定义:

    struct 结构体名

    {

      数据类型 成员1 ;

      数据类型 成员2;

      .......

    };//分号绝对不能丢

    ps:结构体类型描述是不占内存空间的,所以不能直接赋值.

    简单的定义与引用:

    #include <stdio.h>
    #include <stdlib.h>
    #define NAMESIZE 10 struct birthday_st { int year ; int month; int day ; }; struct student_st { int id ; char name[NAMESIZE]; struct birthday_st birth ; int chinese ; int math; }; /*也可以这样定义 struct student_st { int id ; char name[NAMESIZE]; struct birthday_st { int year ; int month; int day ; }birth; int chinese ; int math; }; */ int main(void) { //TYPE NAME = VALUE;定义法则 struct student_st stu = {111,"xxx",{2011,11,11},60,100};//直接引用 //打印所有成员信息 printf("%d %s %d-%d-%d %d %d ",stu.id,stu.name,stu.birth.year,stu.birth.month,stu.birth.day,stu.chinese,stu.math); struct student_st *p = &stu;//间接引用 p+1==>跳转一个结构体 printf("%d %s %d-%d-%d %d %d ",p->id,p->name,p->birth.year,p->birth.month,p->birth.day,p->chinese,p->math); exit(0); }

    上述例子:用直接引用和间接引用两种方法将结构体成员定义并打印出来;

    结构体占用内存空间的大小:结构体对齐:addr % sizeof()整除:一般以第一个成员占用内存空间大小为标尺;

    1.student_st stu:直接引用sizeof(stu)修改成员顺序后大小发生变化。//一般以第一个成员占用内存空间大小为标尺;

    可以在结构体做不对齐操作:如下
    struct student_st
    {
        int id ;
        char name[NAMESIZE];
        struct birthday_st birth ;
        int chinese ;
        int math;
    }__attribute__((packed));
    2.student_st *p:间接引用sizeof(p):就是一个指针占用内存的大小8byts

  • 相关阅读:
    刷皇室成员
    python 2.7中matplotlib的所有版本
    Linux命令使用时路径存在空格、特殊符号
    路径名太长导致无法读取文件
    谷歌浏览器打包插件
    Upload 上载新生
    Linux的终端(base),进入base环境
    Ubuntu16.04系统语言设置为中文以及搜狗输入法的安装
    R语言3D图导出矢量图有bug
    将本地文件复制到hadoop文件系统
  • 原文地址:https://www.cnblogs.com/muzihuan/p/5219948.html
Copyright © 2011-2022 走看看