类型是对数据的抽象,类型相同的数据具有相同的表示形式、存储格式以及相关操作。程序中所有的数据类型都必定属于某种数据类型,数据类型可以理解为创建变量的模具,即固定大小内存的别名。
数据类型有以下一些:
对于数据类型的操作,我们可能会用到typedef
关键字,typedef
关键字主要有三个作用:
struct
关键字示例代码如下:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// 起别名
typedef struct Person
{
char *name;
unsigned int age;
} myPerson;
void test01()
{
struct Person p1 = {"弗洛伊德", 100};
printf("p1.name = %s, p1.age = %u\n", p1.name, p1.age);
myPerson p2 = {"迪杰斯特拉", 80};
printf("p2.name = %s, p2.age = %u\n", p2.name, p2.age);
}
// 区分数据类型
void test02()
{
typedef char* PCHAR;
PCHAR pc = "老铁六六六";
char* name, ch;
name = "离离原上草";
ch = 'c';
printf("pc = %s, name = %s, ch = %c\n", pc, name, ch);
}
// 提高移植性
typedef long long MLL;
void test03()
{
MLL a = 10;
MLL b = 40;
printf("a = %lld, b = %lld\n", a, b);
}
int main(int argc, char* argv[])
{
test01();
test02();
test03();
system("pause");
return 0;
}
#endif
在强化阶段,我们还需要更深入的了解关于void
的使用。void的字面意思是“无类型”,void *
是无类型指针,无类型指针可以指向任何类型的数据。void
定义变量是没有任何意义的,但是当你用void
定义变量的时候,编译器会报错。void
的使用以及注意如下:
void
创建变量,因为编译器无法给无类型变量分配内存void
用于限定函数返回值以及函数参数void *
万能指针,可以不通过强制类型转换就转成其它类型指针关于void
使用的示例代码如下:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// 修饰函数的返回值
void test01()
{
printf("hello test01 .. \n");
}
// 修饰函数的参数
void test02(void)
{
printf("hello test02 .. \n");
}
// void * 泛型指针,可以指向任何数据类型
void test03()
{
printf("sizeof (void*) = %u\n", sizeof(void*));
void *p = NULL;
int *pInt = NULL;
char *pChar = NULL;
pChar = (char *)pInt;
pChar = p; // 万能指针,不能通过强制类型转换就成其它类型指针
}
int main(int argc, char* argv[])
{
test01();
test02();
test03();
system("pause");
return 0;
}
#endif
有了数据类型,编译器就能知道对应的变量应该分配多少大小的内存空间,而求这个大小就可以使用sizeof
运算符。sizeof````参数为变量的时候求内存大小的时候可以不用加括号,而当参数为数据类型的时候必须加上括号。关于
sizeof```的使用和注意如下:
sizeof
不是一个函数,而是一个操作符sizeof
的返回值类型为unsigned int
无符号整型sizeof
可以用于统计数组长度对于sizeof
的示例代码如下:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// sizeof是一个操作符,不是函数
void test01()
{
printf("size of int = %u\n", sizeof(int));
char p = '0';
printf("size of p = %u\n", sizeof p);
}
// sizeof的返回值类型是一个无符号整型
void test02()
{
if (sizeof(int)-20 > 0)
{
printf("大于0\n");
}
else
{
printf("小于0\n");
}
}
// sizeof可以用于统计数组的长度
void test03()
{
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
printf("size of arr = %u\n", sizeof arr);
printf("length of arr = %u\n", sizeof arr / sizeof arr[0]);
}
int main(int argc, char* argv[])
{
test01();
test02();
test03();
system("pause");
return 0;
}
#endif
既能读又能写的内存对象称为变量,而一旦初始化后就不能修改的对象称为常量。变量的修改方式有直接修改和间接修改。示例代码如下:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// 变量的修改方式
void test01()
{
// 直接修改
int a = 10;
a = 20;
// 间接修改
int *p = &a;
*p = 30;
}
struct Person
{
char a; // 0 - 3
int b; // 4 - 7
char c; // 8 - 11
int d; // 12 - 15
};
void test02()
{
struct Person p = { 'a', 10, 'b', 20 };
printf("p.d = %d\n", p.d);
// 修改d属性
p.d = 30;
printf("p.d = %d\n", p.d);
char *pp = &p;
*((int*)(pp + 12)) = 66;
printf("*((int*)(pp + 12)) = %d\n", *((int*)(pp + 12)));
printf("p.d = %d\n", p.d);
*((int*)pp + 3) = 88;
printf("*((int*)pp + 3) = %d\n", *((int*)pp + 3));
printf("p.d = %d\n", p.d);
}
int main(int argc, char* argv[])
{
test01();
test02();
system("pause");
return 0;
}
#endif
在程序没有运行之前,或者说在没有加载到内存之前,程序就已经分为了3段信息,分别为代码区(text)、数据区(data)和未初始化数据区(bss)。有人也将data和bss合起来叫做静态区或全局区。
代码区用于存放CPU执行的机器命令,通常代码区是可共享的,使其可共享的目的是对于频繁被执行的程序只需要在内存中有一份代码即可。代码区通常是只读的。
全局初始化数据区/静态区(data),该区包含了在程序中被明确初始化的全局变量、已经初始化的静态变量(包括局部静态变量)和常量数据(如字符串常量)。
未初始化数据区(bss),存入的是全局初始化变量和未初始化的静态变量。未初始化数据区的数据在程序开始执行之前被内核初始化为0或者空(NULL)。
总得来说,源程序代码在变异之后就主要分为了两段:程序指令(代码区)和程序数据(数据区)。
因此,在程序加载到内存前,代码区和全局区(data和bss)的大小就是固定的,程序运行期间不能改变。而在程序加载到内存之后,会增加堆区(heap)和栈区(stack)。
未初始化数据区(bss)和全局初始化数据区/静态数据区(data)里面存储的数据的生存周期为整个程序运行过程。里面存放的数据为全局变量、静态变量、常量。
栈是一种前进后出的数据结构。栈区(stack)由编译器自动分配释放,存放函数的参数值、返回值、局部变量等。在程序运行过程中实时加载和释放。因此局部变量的生存周期为申请到释放该段栈空间。
堆区(heap)的容量远远大于栈区。空间大小由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收。
栈区中需要注意的事项是不要返回局部变量的地址,局部变量在函数执行之后就被释放了,释放的内存就没有操作权限了,如果操作结果位置。下面给出一个关于栈区使用的示例代码:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
int *myFunc()
{
int a = 10;
return &a;
}
void test01()
{
// 局部变量a早已被释放,没有权限访问这块内存空间
int *p = myFunc();
printf("*p = %d\n", *p);
printf("*p = %d\n", *p);
}
char *getString()
{
char str[] = "hello world";
return str;
}
void test02()
{
char *p = NULL;
p = getString();
printf("p = %s\n", p);
}
int main(int argc, char* argv[])
{
test01();
test02();
system("pause");
return 0;
}
#endif
而对于堆区,开辟需要使用malloc
函数,而释放则使用free
函数。需要注意的时候主调函数没有分配内存,被调函数需要用更高级的指针去修饰低级指针进行分配内存。示例代码如下:
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
int *getSpace()
{
int *p = malloc(sizeof(int)* 5);
if (p == NULL)
return NULL;
for (int i = 0; i < 5; i++)
p[i] = i + 100;
return p;
}
void test01()
{
int *p = getSpace();
for (int i = 0; i < 5; i++)
printf("%d ", p[i]);
putchar('\n');
// 手动开辟手动释放
free(p);
p = NULL;
}
void allocateSpace(char *pp)
{
char *tmp = malloc(100);
memset(tmp, 0, 100);
strcpy(tmp, "hello world");
pp = tmp;
}
void test02()
{
char *p = NULL;
allocateSpace(p);
printf("%s\n", p);
}
void allocateSpace1(char **pp)
{
char *tmp = malloc(100);
memset(tmp, 0, 100);
strcpy(tmp, "hello world");
*pp = tmp;
}
void test03()
{
char *p = NULL;
allocateSpace1(&p);
printf("%s\n", p);
}
int main(int argc, char* argv[])
{
test01();
test02();
test03();
system("pause");
return 0;
}
#endif
除了上述之外,还需要注意static
与extern
的区别。static
在运行前分配内存,在程序结束的时候生命周期结束,且本文件内部都可以使用静态变量。extern
可以提高变量的作用域。示例代码如下:
test.c 文件
extern int g_a = 1000; // 在C语言下,全局变量前部都隐式加了关键字extern
main函数所在c文件
#if 0
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// static 静态变量
// 特点:在运行前分配内存,程序运行结束,声明周期结束,在本文件内部都可以使用静态变量
// 全局作用域a
static int a = 10;
void test01()
{
// 局部作用域b
static int b = 20;
}
int main(int argc, char* argv[])
{
// 告诉编译器 下面代码中的g_a不要报错,是外部链接属性,在其它文件中
extern int g_a;
printf("%d\n", g_a);
system("pause");
return 0;
}
#endif
最后说一下常量,首先谈一下关于const修饰的变量,全局变量使用const修饰的时候该变量会位于常量区。在程序中直接修改会失败,间接修改的时候语法会通过,但是运行也会失败,因为const修饰的全局变量会受到常量区的保护。而对于const修饰的局部变量直接修改会失败,间接修改会成功。用const修饰的局部变量是位于栈上的。
对于字符串常量来说,是否能修改由于ANSI C并没有制定出是否可以修改的标准,所以不同的编译器对于字符串常量的处理是不同的。所以尽量不要去修改字符串常量。既然不一样,那么同一个常量的地址是否一样呢?在TC2.0中,同文件字符串常量地址不同;在VS中,字符串常量地址同文件和不同文件都相同;在Dev C++和QT中同文件里面地址常量相同,不同文件地址不同。
常量的示例代码如下:
#if 1
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#include
#include
// const修饰的变量
// 全局变量
const int a = 10; // 常量区,间接修改语法通过,运行失败,因为会受到常量区的保护
void test01()
{
int *p = &a;
*p = 100;
printf("%d\n", a);
}
void test02()
{
const int b = 10; // 放在栈上,通过间接修改是可以成功的
int *p = &b;
*p = 20;
printf("%d\n", b);
// 在C语言中,const修饰的局部变量称为伪常量,不可以初始化数组
}
void test03()
{
char *p1 = "hello world";
char *p2 = "hello world";
char *p3 = "hello world";
printf("%p\n", &"hello world");
printf("%p\n", p1);
printf("%p\n", p2);
printf("%p\n", p3);
}
void test04()
{
char *str = "hello world";
str[0] = 'x';
}
int main(int argc, char* argv[])
{
//test01();
//test02();
test03();
//test04();
system("pause");
return 0;
}
#endif