C(三)

void test1() {
    int arr1[10];
    printf("%d\n", sizeof(arr1));
    for (int i = 0; i < sizeof(arr1) / sizeof(int); i++) { // 计算数组长度.. 粗糙的做法
        arr1[i] = i;
    }
    for (int i = 0; i < sizeof(arr1) / sizeof(int); i++) {
        printf("%d\n", arr1[i]);
    }
}
  1. 结构体
  • 结构体做形参
#include<stdio.h>
#include<string.h>
struct Book
{
    char title[50];
    char author[50];
    char subject[100];
    int bookId;
};
void printBookInfo(struct Book book);
void printBookInfo2(struct Book* book);

int main() {
    struct Book book1;
    strcpy(book1.title, "C Programming");
    strcpy(book1.author, "Nuha Ali");
    strcpy(book1.subject, "C Programming Tutorial");
    book1.bookId = 100001;
    // printBookInfo(book1);
    printBookInfo2(&book1);
    getchar();
}

void printBookInfo(struct Book book) {
    printf("%s\n", book.title);
    printf("%s\n", book.author);
    printf("%s\n", book.subject);
    printf("%d\n", book.bookId);
}

void printBookInfo2(struct Book* book) {
    printf("%s\n", book->title); // 用"->"
    printf("%s\n", book->author);
    printf("%s\n", book->subject);
    printf("%d\n", book->bookId);
}
  • 位域
struct k{
    int a:1;
    int  :2;    /* 该 2 位不能使用 */
    int b:3;
    int c:2;
};
int main() {
    struct bs {
        unsigned a: 1;
        unsigned  : 7;
        unsigned b: 3;
        unsigned c: 5;
    } bit, *pbit;
    bit.a = 1;
    bit.b = 7;
    bit.c = 15;
    printf("%d,%d,%d\n", bit.a, bit.b, bit.c);
    pbit = &bit;
    pbit->a = 1;
    pbit->b = 3;
    pbit->c = 25;
    printf("%d,%d,%d\n", pbit->a, pbit->b, pbit->c);
    printf("%d,%d,%d\n", bit.a, bit.b, bit.c);
    printf("%d\n", (&bit == pbit));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值