结构体struct、枚举enum、联合体union、位字段、自定义类型typedef、字节对齐

本文介绍了C语言中的结构体struct、枚举enum、联合体union、位字段以及自定义类型typedef的概念和使用。结构体可以作为函数参数传递,可以创建结构体数组和自引用结构体。枚举提供了一种定义符号常量的方式。联合体允许存储不同类型的数据,并在不确定类型时提供兼容性。位字段用于节省内存,可以定义数据的特定位。自定义类型typedef提高了代码的可读性和可移植性。最后,文章讨论了字节对齐的原则和其在不同系统中的表现。
摘要由CSDN通过智能技术生成

1.结构体struct

1.1 结构体的基本知识

#include <stdio.h>

struct point{
        int x;
        int y;
}p1, p2, p3;
struct point pt;
struct point pt2 = {300, 400};
struct point pt3 = {.x = 500};

struct rect {
        struct point pt1;
        struct point pt2;
};
struct rect screen;

int main()
{
        printf("pt2.x = %d, pt2.y = %d\n", pt2.x, pt2.y);
        printf("pt3.x = %d, pt3.y = %d\n", pt3.x, pt3.y);

        double dist, sqrt();
        dist = sqrt((double)pt2.x*pt2.x + (double)pt2.y*pt2.y);
        printf("dist = %lf \n", dist);

        printf("screen.pt2.x = %d \n", screen.pt2.x);

        return 0;
}
huashidazhongbeitushuguan5deiMac:wufile duzhe$ vim struct.c
huashidazhongbeitushuguan5deiMac:wufile duzhe$ gcc struct.c 
huashidazhongbeitushuguan5deiMac:wufile duzhe$ ./a.out 
pt2.x = 300, pt2.y = 400
pt3.x = 500, pt3.y = 0
dist = 500.000000 
screen.pt2.x = 0 
huashidazhongbeitushuguan5deiMac:wufile duzhe$ 

1.2 结构体与函数

  • 传递结构体的每个成员变量;
  • 传递整个机构体;
  • 传递指向结构体的指针;
#include <stdio.h>

struct point{int x; int y;};
struct rect {
        struct point pt1;
        struct point pt2;
};

struct rect screen;
struct point midlle;
struct point makepoint(int, int);

int main()
{
        #define XMAX 300
        #define YMAX 400
        screen.pt1 = makepoint(0, 0);
        screen.pt2 = makepoint(XMAX, YMAX);

        struct point origin = {30}, *pp;
        origin.y = 40;
        //origin = {.x = 30, .y = 40}; //error
        //origin = {30, 40}; //error
        pp = &origin;
        printf("origin: .x,.y is (%d, %d)\n", origin.x, origin.y);
        printf("origin: (*pp).x is (%d, %d)\n", (*pp).x, (*pp).y);
        printf("origin: pp->x   is (%d, %d)\n\n", pp->x, pp->y);

        return 0;
}

struct point makepoint(int x, int y)
{
        struct point temp;

        temp.x = x;
        temp.y = y;
        return temp;
}
/*
struct point addpoint(struct point p1, struct point p2)
{
        p1.x += p2.x;
        p1.y += p2.y;
        return p1;
}

int pt_in_rect(struct point p, struct rect r)
{
        return p.x >= r.pt1.x && p.x <= r.pt2.x
            && p.y >= r.pt1.y && p.y <= r.pt2.y;
}

#define min(a,b) ((a) < (b) ? (a) : (b))
#define max(a,b) ((a) > (b) ? (a) : (b))
// 矩形规范化
struct rect canon_rect(struct rect r)
{
        struct rect temp;

        temp.pt1.x = min(r.pt1.x, r.pt2.x);
        temp.pt1.y = min(r.pt1.y, r.pt2.y);
        temp.pt2.x = max(r.pt1.x, r.pt2.x);
        temp.pt2.y = max(r.pt1.y, r.pt2.y);
        return temp;
}
*/
struct  rect r, *rp = &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值