C语言设计新思维分享
生活随笔
收集整理的這篇文章主要介紹了
C语言设计新思维分享
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
沒有任何套路,直接獲取資源
C語(yǔ)言已經(jīng)有幾十年的歷史了,經(jīng)過長(zhǎng)時(shí)間的發(fā)展和普及,C語(yǔ)言的應(yīng)用場(chǎng)景也有了很大的變化,一些的老的觀念已經(jīng)不在適用,在這里給大家推薦一本講C語(yǔ)言特別好的書,《C語(yǔ)言設(shè)計(jì)新思維》,沒有任何套路直接下發(fā)領(lǐng)取。
C語(yǔ)言設(shè)計(jì)新思維
根據(jù)調(diào)用生成不同的函數(shù)
#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 {//匿名結(jié)構(gòu)體,定義之后相當(dāng)于將原有的結(jié)構(gòu)體成員直接放到這個(gè)地方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)); }匿名聯(lián)合體與匿名結(jié)構(gòu)體的集合
/* 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標(biāo)志,那么就是弱模式了。它不允許我
們使用匿名的結(jié)構(gòu)標(biāo)識(shí)符來引用我們以前定義過的結(jié)構(gòu),相反,它要求
結(jié)構(gòu)必須在本地定義。這樣,我們需要復(fù)制和粘貼整個(gè)P2 struct的定義
了。
總結(jié)
以上是生活随笔為你收集整理的C语言设计新思维分享的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 知识主题间先序关系挖掘
- 下一篇: 5G领域最权威绿宝书迎来中文版啦!