每日三百行代码 第六天
- C 结构体
- 位域
- C 共用体
- C typedef
C结构体
struct name{
int i;
float a;
double c;
char b;
int m[10];
....
}zname;
struct name{
int i;
double a;
char c;
};
struct name t1,t2[10],*t3;
struct COMPLEX{
char string[100];
struct SIMPLE a;
};
struct NODE{
char string[100];
struct NODE *next_node;
};
struct books{
char name[100];
char author[100];
int cost;
} book={
"goodstudy","eternity","100"};
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main(){
struct Books book;
strcpy(book.title,"goodstudy");
strcpy(book.author,"eternity");
strcpy(book.subject,"abc");
book.book_id=123456;
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
return 0;
}
struct Books *struct_pointer;
struct_pointer = &book;
struct_pointer->title;
#include<stdio.h>
#include<string.h>
struct Books{
char title[100];
char author[100];
char subject[100];
int book_id;
};
void printBook(struct Books *book){
printf("Book title: %s\n",book->title);
printf("Book author: %s\n",book->author);
printf("Book subject: %s\n",book->subject);
printf("Book book_id: %d\n",book->book_id);
}
int main(){
struct Books Book1;
strcpy(Book1.title,"goodstudy");
strcpy(Book1.author,"eternity");
strcpy(Book1.subject,"abc");
Book1.book_id=123456;
printBook(&Book1);
return 0;
}
位域
有些信息在存储时,并不需要占用一个完整的字节
而只需要占几个或一个二进制位
如在存放一个开关量时,只有0和1两种状态,用一位二进位即可
为了节省存储空间,并使处理简便,C语言又提供了一种数据结构
称为"位域"或"位段"
所谓"位域"是把一个字节中的二进位划分为几个不同的区域
并说明每个区域的位数
每个域有一个域名,允许在程序中按域名进行操作
这样就可以把几个不同的对象用一个字节的二进制位域来表示
典例:
struct bs{
int a:8;
int b:2;
int c:6;
}data;
struct bs{
unsigned a:4;
unsigned :4;
unsigned b:4;
};
#include<stdio.h>
int main(){
struct bs{
unsigned a:1;
unsigned b:3;
unsigned c:4;
}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=0;
pbit->b&=3;
pbit->c|=1;
printf("%d %d %d\n",pbit->a,pbit->b,pbit->c);
return 0;
}
C 共用体
union Book{
int i;
float f;
char str[100];
}data;
#include<stdio.h>
#include<string.h>
union Data{
int i;
float f;
char str[100];
};
int main(){
union Data data;
printf("Memory size occupied by data: %d\n",sizeof(data));
return 0;
}
#include<stdio.h>
#include<string.h>
union Data{
int i;
float f;
char str[100];
};
int main(){
union Data data;
data.i=10;
data.f=100.25;
strcpy(data.str,"eternity");
printf("%d %f %s\n",data.i,data.f,data.str);
return 0;
}
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
printf( "data.i : %d\n", data.i);
data.f = 100.25;
printf( "data.f : %f\n", data.f);
strcpy( data.str, "eternity");
printf( "data.str : %s\n", data.str);
return 0;
}
C typedef
typedef unsigned char ASD;
ASD b1,b2;
#include <stdio.h>
#include <string.h>
typedef struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
}Book;
int main(){
Book book;
strcpy(book.title,"goodstudy");
strcpy(book.author,"eternity");
strcpy(book.subject,"abc");
book.book_id=123456;
printf( "Book title : %s\n", book.title);
printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);
return 0;
}
#include<stdio.h>
#define TRUE 1
#define FALSE 0
int main(){
printf("%d %d\n",TRUE,FALSE);
return 0;
}