来自于《你必须知道的495个C语言问题》
经过了总结
1-使用typedef定义新的类型名,int16和int32,再根据实际机器选择int, short, long
标准头文件 inttypes.h 已经帮你定义了int16_t
,uint32_t
等等
2- C99中 long long至少64位
3- 存储类型 extern的作用?
extern对数据声明才有意义,对函数声明仅仅是告诉你该函数的定义可能在另一个源文件中
4- auto 毫无作用已经过时
5- 不能在typedef定义之前使用该类型名
如下是错误的:
typedef struct{
char * item;
NODEPTR next;
} *NODEPTR;
正确的方式:
- 使用结构标记
typedef struct node{ //使用结构标记
char * item;
struct node * next;
} *NODEPTR;
- 先typedef
typedef struct node *NODEPTR;
struct node{
char * item;
NODEPTR next;
};
6- 如何定义一组互相引用的结构?
struct b;//提前声明
struct a{
struct b *bptr;
};
struct b{
struct a *aptr;
};
7- typedef int (*funcptr)(); 是什么意思?作用?
funcptr是函数指针,指向的函数返回值为int。
这样的好处是:
在定义函数指针时: funcptr fp1, fp2;
比表达式int (*fp1)(), (*fp2)();
更加便于理解
8- 在一个文件中extern的数组,如何获得该数组的大小
例如:
file1: int array[] = {1, 2, 3};
file2: extern int array[];
在file2中使用sizeof array
是获得不了数组的大小的,会出错
获取数组大小的三种方法
- 在定义数组的文件file1中使用
int arraysz = sizeof(array)
来保存数组大小。然后在file2使用extern int arraysz;
来获取大小 - 定义准确无误的常量
file1中#define ARRAYSZ 3
,然后int array[ARRAYSZ];
在file2中extern int array[ARRAYSZ];
- 数组的最后一个元素放入哨兵(0、-1、NULL)
file1:int array[] = {1, 2, 3, -1};
file2:extern int array[];