《C程式語言》讀書筆記(六) 結構

2021-07-31 09:44:55 字數 4630 閱讀 4555

結構的初始化只能緊跟在結構型別的變數宣告後面,必須對所有結構成員進行初始化,且初始化的值必須為常量。比如

struct abc

x = ;

或者

struct abc y = ;
或者

struct abc z;

z.a = 200;

z.b = 300;

而不允許

struct abc y;

y = ;

1.

在函式中可以呼叫結構,也可以在乙個函式中返回乙個結構。這裡要注意的是,在函式的宣告中如果出現結構的形式,其型別是struct + 結構標記,如struct abc。下面給出課本上兩個使用結構的函式:

struct point makepoint (int x,int y)  //返回型別為struct point

struct point addpoint(struct point p1,struct point p2) //形參為兩個struct point型別,返回為struct point型別

2. 結構指標

在結構很大的條件下,使用指標方式的結構比普通的結構效率更高。結構指標的定義方式和普通指標類似:struct point *pp;。呼叫結構成員時可使用兩種方法:(*pp).app->a。個人感覺第二種方法不容易出錯,因為運算子.的優先順序高於*,所以第一種方法中圓括號不可少,而第二種方法簡單直觀。實際上,通過查詢2.12的運算子優先順序表可以看到,->.的優先順序是最高的,所以在類似於++*等操作時一定注意括號的使用。

在結構的宣告中不能包括結構本身,但是可以包括指向本結構的指標。在下面這個課本例子對於結構的宣告中我們可以看到這一點。

/*例6.5 統計單詞出現的次數完整版***/

#include

#include

#include

#define maxwordnum 100 //輸入字串最大數目

#define maxwordlen 10 //輸入每個字串最大長度

struct tnode

;struct tnode * addtree(struct tnode *p, char *word) //判斷乙個單詞是否在二叉樹中並新增其到合適位置,返回根節點

else

if ((strcmp(p->tword, word)) == 0)

p->num++;

else

if ((strcmp(p->tword, word)) > 0)

p->left = addtree(p->left, word); //遞迴呼叫

else p->right = addtree(p->right, word);

return p;

}int findtree(struct tnode *p,char *word) //查詢乙個單詞是否在二叉樹中,找到返回1,否則返回0

return result;

}void printtree(struct tnode *p) //列印整個二叉樹

}int main(int argc, char

const *argv)

while (*wordarray[i - 1] != '\0' && i < maxwordnum);

struct tnode *root;

root = null;

for (j = 0; j < ((*wordarray[i - 1] == '\0' ? i - 1 : i); j++)

root = addtree(root, wordarray[j]); //建立整棵二叉樹。注意每乙個addtree的呼叫返回的是它本身,所以root並不會因此

printf("press 1 to find a specific word,press 2 to print all words existed:\n");

temp = (char *)malloc(sizeof(char));

gets(temp);

switch(*temp)

return

0;}

給出乙個課本例題的稍微改動和新增版本,其中包含了hash表的初始化、新增資料、刪除和查詢功能,也包括使用malloc()和get_s()為字串指標申請記憶體以及一些程式設計中容易出錯的地方(花了我快一天啊啊啊效率太低了(╯°口°)╯(┴—┴)

//hash表的初始化、新增、刪除和查詢練習

//#include "stdafx.h"

#include

#include

#include

#define maxhashsize 20

#define maxcharsize 20

#define success 1

#define fail -1

typedef

struct tnode

hashtable;

int inithashtable(hashtable *);

int addtohashtable(hashtable *, char*);

int deletehashtable(hashtable *, char *);

int findinhashtable(hashtable *, char *);

int inithashtable(hashtable *root);

unsigned

int calchashvalue(char *);

int inithashtable(hashtable *root) //初始化hash表,所有字串指標指向null,計數為0

return success;

}int addtohashtable(hashtable *root, char *data) //將乙個字串加入hash表

else

hashvalue = (hashvalue + i) % maxhashsize; //存放位置加1

i++;

}if (i == maxhashsize)

else

}int deletehashtable(hashtable *root, char *word) //從hash表中刪除乙個字串資料

else

}int findinhashtable(hashtable *root, char *word) //在hash表中查詢某個字串

unsigned

int calchashvalue(char *data) //計算乙個字串hash的值

int main(int argc, char

const *argv)

while (**(temp + i - 1) != '\0' && i < maxhashsize);

if (inithashtable(root) == success)

printf("initial complete!\n");

else

//新增字串資料過程

for (j = 0; j < (**(temp + i - 1) == '\0' ? i - 1 : i); j++)

}printf("add complete!\n");

//查詢,刪除或退出

while (1)

}return

0;}

格式:typedef 原型別名 新型別名

例子:

1.typedef int length;

2.typedef char *string;定義string為字串指標型別

3.typedef struct tnode{} tree;定義tree為struct tnode型別,因此tree tnode1等價於struct tnode tnode1;

4.typedef struct tnode *ptree;定義ptree為指向struct tnode型別的指標,因此可用:ptree ptree1; ptree1->member訪問成員變數

5.typedef int (*func1)(char*,char*);定義乙個指向函式的指標,這個函式返回char型別,因此可用:func1 func1; a = (*func1)(str1,str2);來呼叫這個函式

共用體(union)簡介

位字段的作用是將多個物件儲存在同乙個機器字中,機器字是指計算機一次運算能夠同時處理的最大位數,通常所說的32/64位電腦就是指機器字的長度。位字段在乙個結構中宣告,方式為:

//**引用自:

struct

flags;

因此,在對每個字段進行賦值的時候,要注意不能超過定義的最大範圍,在上例中範圍為0~2^(n-1)

更詳細的介紹請參考:

《c程式語言》讀書筆記

舉例如下 char a 10 1 定義的時候直接用字串賦值 char a 10 hello 注意 不能先定義再給它賦值,如 char a 10 a 10 hello 這樣是錯誤的!2 對陣列中字元逐個賦值 char a 10 3 利用strcpy char a 10 strcpy a,hello 易...

《C程式語言》 讀書筆記

在mac下可以使用sublime進行c語言程式編寫,然後通過命令列來執行c程式。在sublime新建乙個c程式,譬如 test.c include main 然後在命令列中執行 cc test.c編譯後會自動生成乙個a.out檔案,然後我們執行a.out檔案 test.c就會在命令行內列印出 hel...

C程式語言讀書筆記 5

double dp,atof char 表明,dp 和atof char 的值都是 double 型別的。該宣告中 atof 的引數是指向 char 型別的指標,指標必須指向特性型別的物件,但是 void 型別的指標可以存放指向任何型別的指標。陣列和指標的關係 陣列名所代表的指標就是該陣列最開始的乙...