结构和联合

1-结构体

char *ptr;
ptr="hello";//可行

char str[32];
str="hello";//不可行

在这里插入图片描述

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

//声明结构体
struct student//student结构体名
{
        char name[32];
        //不要写成char name[32]={0},否则是定义,不是声明
        int age;
        char sex;
};
int main()
{
        struct student s1;//定义结构体变量
        struct student s2={"aaaa",20,'m'};//初始化结构体变量
        strcpy(s1.name,"bbbb");
        s1.age=22;
        s1.sex='m';
        //打印结构体变量,逐个打印
        printf("%s %d %c\n",s1.name,s1.age,s1.sex);//通过结构体变量访问结构体成员
        printf("%s %d %c\n",s2.name,s2.age,s2.sex);

        struct student *s3;//结构体指针
        s3=(struct student *)malloc(sizeof(struct student));
        printf("%d\n",sizeof(struct student));

        strcpy(s3->name,"cccc");
        s3->age=24;
        s3->sex='m';
        printf("%s %d %c\n",s3->name,s3->age,s3->sex);

        scanf("%s",s1.name);
        scanf("%s",s3->name);//name是数组名,本身就是地址,不需要取地址
        scanf("%d",&s3->age);

        free(s3);

        return 0;
}

编译运行

[root@localhost 213]# gcc 1-结构体.c -o 1-结构体
[root@localhost 213]# ./1-结构体 
bbbb 22 m
aaaa 20 m
40
cccc 24 m

2-结构体长度

在这里插入图片描述
偏移量计算
例:int a;
char b;
short c;
char d;
int e;

#include<stdio.h>

//1.结构体总长度一定是最长成员的整数倍
//2.每个成员的偏移量一定是该成员长度的整数倍,double偏移量是4的整数倍
struct test
{
        int a;
        char b;
};
typedef struct test t;

struct test2
{
        short a;
        t t1;
        char c;
};

int main()
{
        int a=1;
        printf("%p\n",&a);//int4个字节,a的地址一定是4的倍数
        printf("%d\n",sizeof(t));
        printf("%d\n",sizeof(struct test2));
        return 0;
}

运行

[root@localhost 213]# gcc 2-结构体长度.c -o 2-结构体长度
[root@localhost 213]# ./2-结构体长度 
0xbfcfd500
8
16

3-结构体数组

#include<stdio.h>
#include<stdlib.h>

struct student
{
        char name[32];
        int age;
};
typedef struct student stu;

int main()
{
        stu s[10];//结构体数组

        int i;
        for(i=0;i<3;i++)
        {
                scanf("%s%d",s[i].name,&s[i].age);
        }

        for(i=0;i<3;i++)
        {
                printf("%s%d\n",s[i].name,s[i].age);
        }
        stu *s1[10];//结构体指针数组
        for(i=0;i<3;i++)
        {
                s1[i]=(stu *)malloc(sizeof(stu));
                scanf("%s%d",s1[i]->name,&s1[i]->age);
                printf("%s%d",s1[i]->name,s1[i]->age);
        }


}

运行

[root@localhost 213]# gcc 3-结构体数组.c -o 3-结构体数组
[root@localhost 213]# ./3-结构体数组 
aaa 1
bbb 2
ccc 3
aaa1
bbb2
ccc3
ddd 4
ddd4

4-建议通讯录

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct Info
{
        char name[32];//char *name记得malloc申请空间
        char tel[32];
        int age;
};
typedef struct Info info;

#define size 100

int person=0;//表示已经添加的人数

void welcome()
{
        system("clear");
        printf("\n\n\n");
        printf("\t\t********************************\n");
        printf("\t\t********************************\n");
        printf("\t\t           欢迎.................\n");
        printf("\t\t********************************\n");
        printf("\t\t********************************\n");
        sleep(1);
}

void menu()
{
        system("clear");
        printf("\n\n\n");
        printf("\t\t********************************\n");
        printf("\t\t********************************\n");
        printf("\t\t1.增加信息       2.修改信息\n");
        printf("\t\t3.查找信息       4.删除信息\n");
        printf("\t\t5.显示信息       6.退出\n");
        printf("\t\t********************************\n");
        printf("\t\t********************************\n");
        sleep(1);
}

void addinfo(info *i)//添加信息
{
        char name[32]={0};
        char tel[32]={0};
        int age=0;

        printf("请输入姓名、电话、年龄:\n");
        scanf("%s%s%d",name,tel,&age);

        strcpy(i[person].name,name);
        strcpy(i[person].tel,tel);
        i[person].age=age;

        person++;
}

void modifyinfo(info *i)//修改信息
{
        int num;
        printf("修改几号信息:\n");
        scanf("%d",&num);

        char name[32]={0};
        char tel[32]={0};
        int age=0;

        printf("请输入姓名、电话、年龄:\n");
        scanf("%s%s%d",name,tel,&age);

        strcpy(i[num-1].name,name);
        strcpy(i[num-1].tel,tel);
        i[num-1].age=age;
}

void searchinfo(info *i)//查找信息
{
        int num;
        printf("查找几号信息:\n");
        scanf("%d",&num);
        printf("%s %s %d\n",i[num-1].name,i[num-1].tel,i[num-1].age);
        sleep(2);
}

void deleteinfo(info *i)//删除信息
{
        int num;
        printf("删除几号信息:\n");
        scanf("%d",&num);

        int k;
        for(k=num-1;k<(person-1);k++)
        {
                strcpy(i[k].name,i[k+1].name);
                strcpy(i[k].tel,i[k+1].tel);
                i[k].age=i[k+1].age;
        }
        person--;

}
void showinfo(info *i)//显示信息
{
        int j;

        for(j=0;j<person;j++)
        {
                printf("%s %s %d\n",i[j].name,i[j].tel,i[j].age);
        }
        sleep(2);

}

int main()
{
        int choice;
        info in[size]={0};
        welcome();
        while(1)
        {
                menu();
                scanf("%d",&choice);

                switch(choice)
                {
                        case 1:
                                addinfo(in);
                                break;
                        case 2:
                                modifyinfo(in);
                                break;
                        case 3:
                                searchinfo(in);
                                break;
                        case 4:
                                deleteinfo(in);
                                break;
                        case 5:
                                showinfo(in);
                                break;
                        case 6:
                                exit(0);//退出进程
                        default:
                                printf("error!");
                }
        }
}

运行

        ********************************
        ********************************
        1.增加信息       2.修改信息
        3.查找信息       4.删除信息
        5.显示信息       6.退出
        ********************************
        ********************************

5-联合体概念

#include<stdio.h>

//联合体特点:所有成员共享同一段内存空间  联合体常诉:最长成员长度
union test
{
        int a;
        int b;
        char c;
};

int main()
{
        union test t;
        printf("%d\n",sizeof(t));
        t.a=100;
        printf("%d\n",t.b);
        return 0;

}

运行

[root@localhost 213]# ./5-联合体概念 
4
100

6-判断大小端(面试题)

在这里插入图片描述

#include<stdio.h>

union test
{
        short val;
        char ch[sizeof(short)];

};

int main()
{
        union test t;

        t.val=0x0102;
        if(t.ch[0]==1&&t.ch[1]==2)
        {
                printf("大端字节序\n");
        }
        else if(t.ch[0]==2&&t.ch[1]==1)
        {
                printf("小端字节序\n");
        }

        return 0;
}

运行

[root@localhost 213]# ./6-判断大小端 
小端字节序

7-大小端转换

在这里插入图片描述

#include<stdio.h>
int main()
{
        int a=1;

        printf("%d\n",((a&0x000000ff)<<24)|
                                        ((a&0x0000ff00)<<8)|
                                        ((a&0x00ff0000)>>8)|
                                        ((a&0xff000000)>>24));
}

运行

[root@localhost 213]# ./7-大小端转换 
16777216

8-枚举

#include<stdio.h>
/*
#define SUN 0
#define MON 1
#define TUE 2
#define WEN 3
*/

enum
{
        SUN,
        MON,
        TUE,
        WEN
};

int main()
{
        printf("%d\n",SUN);
        printf("%d\n",MON);
        printf("%d\n",TUE);
        printf("%d\n",WEN);
        return 0;
}

运行

[root@localhost 213]# ./8-枚举 
0
1
2
3
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值