C语言-构造类型
几篇文章复习C语言,摘录重点,仅用于个人学习~
数组
所有的数组都是由连续的内存位置组成。最低的地址对应第一个元素,最高的地址对应最后一个元素。
double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};
字符串
字符串实际上是使用 null 字符 \0 终止的一维字符数组
char site[7] = {'R', 'U', 'N', 'O', 'O', 'B', '\0'}; <=> char site[] = "RUNOOB";
枚举
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
};
//注意:第一个枚举成员的默认值为整型的 0,后续枚举成员的值在前一个成员上加 1。我们在这个实例中把第一个枚举成员的值定义为 1,第二个就为 2,以此类推。
在C 语言中,枚举类型是被当做 int 或者 unsigned int 类型来处理的,所以按照 C 语言规范是没有办法遍历枚举类型的。
不过在一些特殊的情况下,枚举类型必须连续是可以实现有条件的遍历。
#include <stdio.h>
enum DAY
{
MON=1, TUE, WED, THU, FRI, SAT, SUN
} day;
int main()
{
// 遍历枚举元素
for (day = MON; day <= SUN; day++) {
printf("枚举元素:%d \n", day);
}
//枚举元素:1
//枚举元素:2
//枚举元素:3
//枚举元素:4
//枚举元素:5
//枚举元素:6
//枚举元素:7
结构体
用户自定义的可用的数据类型,它允许您存储不同类型的数据项
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
struct Books Book1; /* 声明 Book1,类型为 Books */
struct Books Book2; /* 声明 Book2,类型为 Books */
/* Book1 详述 */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* Book2 详述 */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* 输出 Book1 信息 */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* 输出 Book2 信息 */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
//Book 1 title : C Programming
//Book 1 author : Nuha Ali
//Book 1 subject : C Programming Tutorial
//Book 1 book_id : 6495407
//Book 2 title : Telecom Billing
//Book 2 author : Zara Ali
//Book 2 subject : Telecom Billing Tutorial
//Book 2 book_id : 6495700
结构体的成员可以包含其他结构体,也可以包含指向自己结构体类型的指针,而通常这种指针的应用是为了实现一些更高级的数据结构如链表和树等
//此结构体的声明包含了其他的结构体
struct COMPLEX
{
char string[100];
struct SIMPLE a;
};
//此结构体的声明包含了指向自己类型的指针
struct NODE
{
char string[100];
struct NODE *next_node;
};
共用体
共用体是一种特殊的数据类型,允许您在相同的内存位置存储不同的数据类型。您可以定义一个带有多成员的共用体,但是任何时候只能有一个成员带有值。共用体提供了一种使用相同的内存位置的有效方式。
union Data
{
int i;
float f;
char str[20];
} data;
现在,Data 类型的变量可以存储一个整数、一个浮点数,或者一个字符串。这意味着一个变量(相同的内存位置)可以存储多个多种类型的数据。您可以根据需要在一个共用体内使用任何内置的或者用户自定义的数据类型。
共用体占用的内存应足够存储共用体中最大的成员。例如,在上面的实例中,Data 将占用 20 个字节的内存空间,因为在各个成员中,字符串所占用的空间是最大的。
位域
所谓"位域"是把一个字节中的二进位划分为几个不同的区域,并说明每个区域的位数。每个域有一个域名,允许在程序中按域名进行操作。这样就可以把几个不同的对象用一个字节的二进制位域来表示。(1个字节等于8个二进制位)
如果程序的结构中包含多个开关量,只有 TRUE/FALSE 变量
#include <stdio.h>
#include <string.h>
/* 定义简单的结构 */
struct
{//这种结构需要 3*4=12 字节的内存空间
unsigned int test1Validated;
unsigned int test2Validated;
unsigned int test3Validated;
} status1;
/* 定义位域结构 */
struct
{//status 变量将占用 4 个字节的内存空间,但是只有 2 位被用来存储值。如果您用了 32 个变量,每一个变量宽度为 1 位,那么 status 结构将使用 32个二进制位=4 个字节; 但只要您再多用一个变量,如果使用了 33 个变量,那么它将分配内存的下一段来存储第 33 个变量,这个时候就开始使用 8 个字节
unsigned int test1Validated : 1;
unsigned int test2Validated : 1;
unsigned int test3Validated : 1;
unsigned int test4Validated : 1;
unsigned int test5Validated : 1;
unsigned int test6Validated : 1;
unsigned int test7Validated : 1;
unsigned int test8Validated : 1;
unsigned int test9Validated : 1;
unsigned int test10Validated : 1;
unsigned int test11Validated : 1;
unsigned int test12Validated : 1;
unsigned int test13Validated : 1;
unsigned int test14Validated : 1;
unsigned int test15Validated : 1;
unsigned int test16Validated : 1;
unsigned int test17Validated : 1;
unsigned int test18Validated : 1;
unsigned int test19Validated : 1;
unsigned int test20Validated : 1;
unsigned int test21Validated : 1;
unsigned int test22Validated : 1;
unsigned int test23Validated : 1;
unsigned int test24Validated : 1;
unsigned int test25Validated : 1;
unsigned int test26Validated : 1;
unsigned int test27Validated : 1;
unsigned int test28Validated : 1;
unsigned int test29Validated : 1;
unsigned int test30Validated : 1;
unsigned int test31Validated : 1;
unsigned int test32Validated : 1;
// unsigned int test33Validated : 1;
} status2;
int main()
{
printf( "Memory size occupied by status1 : %lu\n", sizeof(status1));
printf( "Memory size occupied by status2 : %lu\n", sizeof(status2));
return 0;
}
//Memory size occupied by status1 : 12
//Memory size occupied by status2 : 4