c结构体和共用体

结构体:
类型描述
struct 结构体名
{
数据类型 成员1;
数据类型 成员2;
.............

};
结构体名 可省略
存储特点:
各种不同类型数据在内存中线性存放
地址对齐(当前平台取址 32 位4个字节),地址偏移

#include <stdio.h>

struct A
{
    int a;
    char ch;
    float b;
};

int main()
{
    printf("sizeof(A)=%d\n",sizeof(struct A));
    printf("sizeof(int )=%d,sizeof(char)=%d,sizeof(float)=%d\n",sizeof(int),sizeof(char),sizeof(float));
}
-------------------
sizeof(A)=12
sizeof(int )=4,sizeof(char)=1,sizeof(float)=4

嵌套定义

#include <stdio.h>

#define NAME_SIZE 32

struct birthday{
    int year;
    int month;
    int day;
};
struct person
{
    int id;
    char name[NAME_SIZE];
    int age;
    struct birthday birth;
    struct 
    {
        char father[16];
        char mother[16]
    } parents;

} p1,p2;
//如果无头,必须一次声明完 变量名
int main()
{
    struct person p3,p4,*p,arr[3];

}

初始化

成员引用
结构体类型变量名
变量名.成员名
指针名->成员名
(*指针名).成员名

#include <stdio.h>

#define NAME_SIZE 32

struct birthday{
    int year;
    int month;
    int day;
};
struct person
{
    int id;
    char name[NAME_SIZE];
    struct birthday birth;
    int age;

};

int main()
{
    struct person p1={.id=1000};
    struct person p2={10010,"php",{2018,1,27},27};

    printf("p1.id=%d\r\n",p1.id);
    printf("p1.age=%d\r\n",p1.age);

    printf("p2.id=%d\n",p2.id );
    printf("p2.name=%s\n",p2.name);
    printf("p2.birth.year=%d\n",p2.birth.year );
    printf("p2.birth.month=%d\n",p2.birth.month );
    printf("p2.age=%d\n",p2.age);
}
-------------------------------------------
p1.id=1000
p1.age=0
p2.id=10010
p2.name=php
p2.birth.year=2018
p2.birth.month=1
p2.age=27

makefile

file=$1
$(file):$(file).o
    gcc  -c $(file).o -o $(file)
clean:
    $(RM) -r *.o 

 

共用体
union 共用体名称
{
数据类型 成员1;
数据类型 成员2;
.............
}
存储特点
同一时刻只能存一个成员存在

以最大的成员变量作为空间大小

#include <stdio.h>

union {
    int i;
    float f;
    char ch;
    double d;
} u1;

int main(){
    u1.i=100;
    printf("u1.d=%f\n",u1.d); //u1.d=0.000000    无意义
   printf("sizeof(u1)=%d\n",sizeof(u1));
}
//高16位和低16位相加
#include <stdio.h>

union {
    struct{
        unsigned short low;
        unsigned short high;
    } x;
    unsigned int y;

} num;

int main(){
    num.y=0x11223344;
    printf("%x\n",num.x.low+num.x.high);//4466

    unsigned int i=0x11223344;
    printf("%x\n", ((i>>16)+i&0xFFFF));//4466
}

成员初始化和引用同结构体

共用体引用

union {
    int i;
    float f;
    char ch;
} num,*p;

int main()
{
    num.i=10;
    p=&num;
    printf("num.i=%d\n",num.i);
    printf("p->i=%d\n",p->i);
}

指针与结构体共用体
值传递,地址传递

#include <stdio.h>

#define NAME_SIZE 32

struct birthday{
    int year;
    int month;
    int day;
};
struct person
{
    int id;
    char name[NAME_SIZE];
    int age;
    struct birthday birth;

} ;
void show_person(struct person p1){
    printf("sizeof(p1)=%d\n",sizeof(p1)); //传参开销 52
    printf("p1.id=%d\n",p1.id );
    printf("p1.name=%s\n",p1.name );
    printf("p1.birth.year=%d\n",p1.birth.year);
}

void show_person1(struct person *p){
    printf("sizeof(p)=%d\n",sizeof(p)); 
    printf("p->id=%d\n",p->id );
    printf("p->name=%s\n",p->name );
    printf("p->birth.year=%d\n",p->birth.year);
}

int main()
{
    struct person p1={1001,"hk",27,{2017,2,27}};
    show_person(p1); //值传递
    printf("---------------------------\n");
    show_person1(&p1);
}
------------------------------
sizeof(p1)=52
p1.id=1001
p1.name=hk
p1.birth.year=2017
---------------------------
sizeof(p)=8
p->id=1001
p->name=hk
p->birth.year=2017