欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

C语言设计新思维分享

发布时间:2025/3/15 编程问答 35 豆豆
生活随笔 收集整理的这篇文章主要介绍了 C语言设计新思维分享 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

没有任何套路,直接获取资源
C语言已经有几十年的历史了,经过长时间的发展和普及,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;

总结

以上是生活随笔为你收集整理的C语言设计新思维分享的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。