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]);
}
}
- 结构体
#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));