C语言结构体

本文详细介绍了C语言中的结构体定义、typedef用法、银行卡属性的设计、内存初始化、拷贝操作、指针以及枚举类型和位段联合体的概念和实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#C语言#   #课堂笔记#

一、结构体的定义

struct student{
        char name[20];
        char sex[];  // male    female
        size_t age;
        float score;
};
struct student  stu={  ,  ,  ,   };
strcpy( stu.name, "list" );
typedef struct student student;
边设计边 命名
typedef struct student{
}student;

二、银行卡

银行卡属性

 银行信息

        money

        id

        passwd

 用户信息

        username 

        sex

        age

        phone

        idcard   ------>身份证     *****20050111**** 获取身份证时间,判断16岁

       

        bankname

        address  开户行

        办卡时间

        卡类型

        流水明细

#include <stdio.h>
typedef struct User {
       char username[10];
       char idcard[20];
       long long phone;
}user;
typedef struct Record {
       char time[30];
       char optype[10];
       float rmony;
}Record;
typedef struct BankCard {
       float money;
       long id;
       int passwd;
       User user;
       char bankname[20];
       char address[20];
       char date[30];    //时间   2024-04-14 星期日 14:46:45
       char type[10];   //储蓄卡  信用卡
       Record records[100];  //100条流水记录
}BankCard;
int main() {
       BankCard card1 = { 0.0,1,1,{"zs","111",123}, };
       return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
typedef struct student {
       char name[10];
       float scord;
}Stu;
int main() {
       Stu stus[3];
       for (int i = 0; i < 3; i++) {
              scanf("%s %f", stus[i].name, stus[i].scord);
    }
       float sum = 0;
       for (int i = 0; i < 3; i++) {
              sum = sum + stus[i].scord;
       }
       return 0;
}

三、结构体数组

Student :   name[20]       age    

Student  stus[3];

 四、内存初始化为0

memset(stus ,0, 3*sizeof(student));

  

五、  拷贝    memcpy

memcpy(stus2,stus,3*sizeof(student));

六、结构体和指针

int val = 10;   int *p=&val;   *p = 10;
Student stu = {"zs",10};
Student *p = &stu;
stu.age = 2 ;
(*p ). age                     优先级    .   >   *
->    指向符     (*p).age      <==>       p -> age
strcpy( p -> name , "list" );

七、枚举类型

enum  BankType={  cxk  , xyk      }; 

sizeof(enum BankType);      4字节  整型

enum  Chesskind = {   BLACK =1  ,    WHITE     ,    BLANK = 0 };

八、位段联合体

位段      节省结构体类型内存
联合体
struct A{
        char c;
        char d;
        char e;
}
#include <stdio.h>
struct A {
       unsigned char id : 4;
       unsigned char level : 4;
};
int main() {
       A a;
       a.id = 0x8;
       a.level = 0x9;
       printf("%d", sizeof(a));
       return 0;
}

位域可以是无名位域


#include <stdio.h>
#include <memory.h>
#include <iostream>
using namespace std;
struct A {
       int a : 5;
       int b : 3;
};
int main(void) {
       char str[100] = "013456789afsadfsdlfjlsdjfl";
       struct A d;
       memcpy(&d, str, sizeof(A));
       cout << d.a << endl;
       cout << d.b << endl;
       return 0;
}
联合体设计
union Uname{
    short s;
    char arr[];
    int val;
}
                最大成员所占内存
struct People{
    char name[20];
    int age;
    union {
    int stuid;
    int tesid;           //联合体共占用一块内存
    };
}
int main()
{
People p={};
return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <memory.h>
union Test {
       int ip;
       unsigned char arr[4];
};
int main() {
       Test t = { 2148205375 };
       char buff[50];
       sprintf(buff, "%d.%d.%d.%d", t.arr[3], t.arr[2], t.arr[1], t.arr[0]);
       printf("%s\n", buff);
       return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值