没有任何套路,直接获取资源
C语言已经有几十年的历史了,经过长时间的发展和普及,C语言的应用场景也有了很大的变化,一些的老的观念已经不在适用,在这里给大家推荐一本讲C语言特别好的书,《C语言设计新思维》,没有任何套路直接下发领取。
书中展现了传统C语言教科书所不具备的最新的相关技术,如果你有一定的C语言基础并且迫切的想提高自己的C语言编程能力,那么推荐你看下。
根据调用生成不同的函数
#define def_object_copy(tname, ...) \
void * tname##_copy(tname *in) { \
tname *out = malloc(sizeof(tname)); \
*out = *in; \
__VA_ARGS__; \
return out; \
}
def_object_copy(keyval) // Expands to the previous declarations of keyval_copy.
#include <stdio.h>
#include <math.h>
typedef struct point {
double x, y;
} point;
typedef struct {
//匿名结构体,定义之后相当于将原有的结构体成员直接放到这个地方
struct point; ❶
double z;
} threepoint;
double threelength (threepoint p){
return sqrt(p.x*p.x + p.y*p.y + p.z*p.z); ❷
}
int main(){
threepoint p = {.x=3, .y=0, .z=4}; ❸
printf("p is %g units from the origin\n", threelength(p));
}
匿名联合体与匿名结构体的集合
/* Compile with:
make LDLIBS='-lm' CFLAGS="-g -Wall -std=gnu11 -fms-extensions" seamlesstwo
*/
#include <stdio.h>
#include <math.h>
typedef struct point {
double x, y;
} point;
typedef struct {
union {
struct point;
point p2;
};
double z;
} threepoint;
double length (point p){
return sqrt(p.x*p.x + p.y*p.y);
}
double threelength (threepoint p){
return sqrt(p.x*p.x + p.y*p.y + p.z*p.z);
}
int main(){
threepoint p = {.x=3, .y=0, .z=4};
printf("p is %g units from the origin\n", threelength(p));
double xylength = length(p.p2);
printf("Its projection onto the XY plane is %g units from the origin\n", xylength);
}
如果不使用-fms-extensions标志,那么就是弱模式了。它不允许我
们使用匿名的结构标识符来引用我们以前定义过的结构,相反,它要求
结构必须在本地定义。这样,我们需要复制和粘贴整个P2 struct的定义
了。
typedef struct {
union {
struct {
double x, y;
};
point p2;
};
double z;
} threepoint;
413

被折叠的 条评论
为什么被折叠?



