C语言结构体学习笔记

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

struct obj {
    char *obj_name;
    double price;
};
struct student {
    int id;
    int age;
    char *name;
    struct obj obj1;

} s = {1, 3, "aa"};

void modify(struct student *p) {
    p->age = 100;
}

union Object {
    int a;
    char b;
    float c;
};

int main() {
    struct student stu = {1, 12, "小明"};
    printf("%d  %d  %s\n", stu.id, stu.age, stu.name);
    struct student stuu = {.name="hf"};
    printf("%s", stuu.name);
    stuu.age = 19;
    printf("%d", stuu.age);

    printf("%d\n", sizeof(int));
    printf("%d\n", sizeof(double));
    int arr[10];
    printf("%d\n", sizeof(arr));
    printf("%d\n", sizeof(arr[0]));
    printf("%d\n", sizeof(struct student));
    printf("%d\n", sizeof(struct obj));

    struct student S[3] = {
            {1,  24,  "sdh",  "pen",     5},
            {23, 45,  "wuli", "pencil",  432},
            {4,  455, "lii",  "lapstop", 234145}
    };

    printf("%\n", S[2].name);
    struct student *p = &S;
    printf("%s\n", (p + 2)->name);
    printf("%s\n", (*(p + 2)).name);

    struct student *pp = &stu;
    modify(&stu);

    printf("%d\n", pp->age);


    printf("\n");
    union Object object;
    object.a = 129;
    printf("%d\n", object.b);

    union Object ob = {.c=23.7};
    union Object *op = &ob;
    printf("%.1f\n", op->c);
    printf("%.1f\n", op->b);
}

测试结果:

D:\CodeCpp\untitled2\cmake-build-debug\untitled2.exe
1  12  小明
hf194
8
40
4
32
16
%
lii
lii
100

-127
23.7
0.0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值