C语言结构体,可谓是C强大功能之一,也是C++语言之所以能衍生的有利条件,事实上,当结构体中成员中有函数指针了后,那么,结构体也即C++中的类了。
C语言中,结构体的声明、定义是用到关键字struct,就像联合体用到关键字union、枚举类型用到enum关键字一样,事实上,联合体、枚举类型的用法几乎是参照结构体来的。结构体的声明格式如下:
struct tag-name{ { member 1; … member N; };
因此,定义结构体变量的语句为:struct tag-name varible-name,如struct point pt;其中,point 为tag-name,pt是结构体struct point变量。当然,也可以一次性声明结构体类型和变量,即如下:struct tag-name {…} x,y,z;就类似于int x,y,z;语句一样。也可以在定义结构体变量时即赋初值,即变量初始化,struct point pt={320,200};
当然,也就可以有结构体指针、结构体数组了。访问结构体变量中的member的方法有:如果是由结构体变量名来访问,则是structure-variable-name.member;如果是由结构体变量指针来访问,则是structure-variable-pointer->member;
好了,上面的不是重点,也不难掌握,只是细节问题。结构体具有重要的应用,如下的:
如自引用的结构体,常用来作为二叉树等重要数据结构的实现:假设我们要实现一个普遍的问题的解决算法——统计某些输入的各单词出现的频数。由于输入的单词数是未知,内容未知,长度未知,我们不能对输入进行排序并采用二分查找。……那么,一种解决办法是:将已知的单词排序——通过将每个到达的单词排序到适当位置。当然,实现此功能不能通过线性排序,因为那样有可能很长,相应地,我们将使用二叉树来实现。该二叉树每一个单词为一个二叉树结点,每个结点包括:
- a pointer to the text of the word
- a count of the number of occurences
- a pointer to the left child node
- a pointer to the right child node
其写在程序中,即:
struct tnode{/*the tree node:*/ char *word;/*points to the next*/ int count;/*number of occurences*/ struct tnode *left;/*left child*/ struct tnode *right;/*right child*/ }
完成上述功能的完整程序如下:
#include<stdio.h> #include<ctype.h> #include<string.h> #include"tNode.h" #define MAXWORD 100 struct tnode *addtree(struct tnode *,char *); void treeprint(struct tnode *); int getword(char *,int); struct tnode *talloc(void); char *strdup2(char *); /*word frequency count*/ main() { struct tnode *root; char word[MAXWORD]; root=NULL; while(getword(word,MAXWORD)!=EOF) if(isalpha(word[0])) root=addtree(root,word); treeprint(root); return 0; } #define BUFSIZE 100 char buf[BUFSIZE];/*buffer for ungetch*/ int bufp=0;/*next free position in buf*/ int getch(void)/*get a (possibly pushed back) character*/ { return (bufp>0)? buf[--bufp]:getchar(); } void ungetch(int c)/*push back character on input*/ { if(bufp>=BUFSIZE) printf("ungetch:too many characters\n"); else buf[bufp++]=c; } /*getword:get next word or character from input*/ int getword(char *word,int lim) { int c,getch(void); void ungetch(int); char *w=word; while(isspace(c=getch() )); if(c!=EOF) *w++=c; if(!isalpha(c)){ *w='\0'; return c; } for(;--lim>0;w++) if(!isalnum(*w=getch())){ ungetch(*w); break; } *w='\0'; return word[0]; } /*addtree:add a node with w,at or below p*/ struct tnode *addtree(struct tnode *p,char *w) { int cond; if(p==NULL){/*a new word has arrived*/ p=talloc();/*make a new node*/ p->word=strdup(w); p->count=1; p->left=p->right=NULL; }else if((cond=strcmp(w,p->word))==0) p->count++;/*repeated word*/ else if(cond<0)/*less than into left subtree*/ p->left=addtree(p->left,w); else /*greater than into right subtree*/ p->right=addtree(p->right,w); return p; } /*treeprint:in-order print of tree p*/ void treeprint(struct tnode *p) { if(p!=NULL){ treeprint(p->left); printf("%4d %s\n",p->count,p->word); treeprint(p->right); } } #include<stdlib.h> /*talloc:make a tnode*/ struct tnode *talloc(void) { return (struct tnode *)malloc(sizeof(struct tnode)); } char *strdup2(char *s)/*make a duplicate of s*/ { char *p; p=(char *)malloc(strlen(s)+1);/*+1 for '\0'*/ if(p!=NULL) strcpy(p,s); return p; }
其中,其它的关于union、enum这里就不多说了,再说一个关于结构体的非常重要的应用——位操作:
当然,我们知道,对于位操作,我们可通过#define tables(即用宏和C中的位操作来实现)
如:
#define KEYWORD 01 /*0001*/ #define EXTERNAL 02 /*0010*/ #define STATIC 04 /*0100*/
或
enum{KEYWORD =01,EXTERNAL =02,STATIC =04};
那么,flags|=EXTERNAL|STATIC;将打开flags的EXTERNAL和STATIC位,而
flags&=~(EXTERNAL|STATIC);将关闭flags的EXTERNAL和STATIC位.
然而,上述定义的位模式可以用结构体如下写:
struct{ unsigned int is_keyword:1; unsigned int is_extern:1; unsigned int is_static:1; }flags;/*This defines a variable called flags that contains three 1-bit fields*/
那么,上述打开相应位的操作为:
flags.is_extern=flags.is_static=1;
上述关闭相应位的操作为:
flags.is_extern=flags.is_static=0;

Python腳本在Unix系統上無法運行的原因包括:1)權限不足,使用chmod xyour_script.py賦予執行權限;2)Shebang行錯誤或缺失,應使用#!/usr/bin/envpython;3)環境變量設置不當,可打印os.environ調試;4)使用錯誤的Python版本,可在Shebang行或命令行指定版本;5)依賴問題,使用虛擬環境隔離依賴;6)語法錯誤,使用python-mpy_compileyour_script.py檢測。

使用Python數組比列表更適合處理大量數值數據。 1)數組更節省內存,2)數組對數值運算更快,3)數組強制類型一致性,4)數組與C語言數組兼容,但在靈活性和便捷性上不如列表。

列表列表更好的forflexibility andmixDatatatypes,何時出色的Sumerical Computitation sand larged數據集。 1)不可使用的列表xbilese xibility xibility xibility xibility xibility xibility xibility xibility xibility xibility xibles and comply offrequent elementChanges.2)

numpymanagesmemoryforlargearraysefefticefticefipedlyuseviews,副本和內存模擬文件.1)viewsAllowSinglicingWithOutCopying,直接modifytheoriginalArray.2)copiesCanbecopy canbecreatedwitheDedwithTheceDwithThecevithThece()methodervingdata.3)metservingdata.3)memore memore-mappingfileShessandAstaStaStstbassbassbassbassbassbassbassbassbassbassbb

Listsinpythondonotrequireimportingamodule,helilearraysfomthearraymoduledoneedanimport.1)列表列表,列表,多功能和canholdMixedDatatatepes.2)arraysaremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremoremeremeremeremericdatabuteffeftlessdatabutlessdatabutlessfiblesible suriplyElsilesteletselementEltecteSemeTemeSemeSemeSemeTypysemeTypysemeTysemeTypysemeTypepe。

pythonlistscanStoryDatatepe,ArrayModulearRaysStoreOneType,and numpyArraySareSareAraysareSareAraysareSareComputations.1)列出sareversArversAtileButlessMemory-Felide.2)arraymoduleareareMogeMogeNareSaremogeNormogeNoreSoustAta.3)

WhenyouattempttostoreavalueofthewrongdatatypeinaPythonarray,you'llencounteraTypeError.Thisisduetothearraymodule'sstricttypeenforcement,whichrequiresallelementstobeofthesametypeasspecifiedbythetypecode.Forperformancereasons,arraysaremoreefficientthanl

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

記事本++7.3.1
好用且免費的程式碼編輯器