C語言struct結(jié)構(gòu)體介紹
struct
C 語言沒有其他語言的對(duì)象(object)和類(class)的概念,struct 結(jié)構(gòu)很大程度上提供了對(duì)象和類的功能。
下面是struct自定義數(shù)據(jù)類型的一個(gè)例子。
struct tag { member-list member-list member-list ... } variable-list;
聲明了數(shù)據(jù)類型car和該類型的變量car。
struct car { char *name; float price; int speed; } mycar; struct car myca = {"大眾", 178.9, 100}; mycar.name = "本田";
如果將 struct 變量傳入函數(shù),函數(shù)內(nèi)部得到的是一個(gè)原始值的副本。
#include <stdio.h> struct turtle { char* name; char* species; int age; }; void happy(struct turtle t) { t.age = t.age + 1; } int main() { struct turtle myTurtle = {"MyTurtle", "sea turtle", 99}; happy(myTurtle); printf("Age is %i\n", myTurtle.age); // 輸出 99 return 0; }
上面示例中,函數(shù)happy()傳入的是一個(gè) struct 變量myTurtle,函數(shù)內(nèi)部有一個(gè)自增操作。但是,執(zhí)行完happy()以后,函數(shù)外部的age屬性值根本沒變。原因就是函數(shù)內(nèi)部得到的是 struct 變量的副本,改變副本影響不到函數(shù)外部的原始數(shù)據(jù)。
指針變量也可以指向struct結(jié)構(gòu)。
struct book { char title[500]; char author[100]; float value; }* b1;
上面示例中,變量b1是一個(gè)指針,指向的數(shù)據(jù)是struct book類型的實(shí)例。
為了使用指向該結(jié)構(gòu)的指針訪問結(jié)構(gòu)的成員,必須使用 -> 運(yùn)算符,如下所示:
b1->title;//9-2.c struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; // 函數(shù)聲明 void printBook(struct Books *books); int main() { struct Books Book1; struct Books Book2; /** * Book1 描述 */ strcpy(Book1.title, "C Programming"); strcpy(Book1.author, "Nuha Ali"); strcpy(Book1.subject, "C Programming Tutorial"); Book1.book_id = 6495407; /* Book2 詳述 */ strcpy(Book2.title, "Telecom Billing"); strcpy(Book2.author, "Zara Ali"); strcpy(Book2.subject, "Telecom Billing Tutorial"); Book2.book_id = 6495700; /* 通過傳 Book1 的地址來輸出 Book1 信息 */ printBook(&Book1); /* 通過傳 Book2 的地址來輸出 Book2 信息 */ printBook(&Book2); return 0; } void printBook(struct Books *book) { printf("Book title : %s\n", book->title); printf("Book author : %s\n", book->author); printf("Book subject : %s\n", book->subject); printf("Book before book_id : %d\n", book->book_id); (*book).book_id = (*book).book_id + 1; printf("Book agter book_id : %d\n", book->book_id); }
struct 結(jié)構(gòu)也可以作為數(shù)組成員。下面示例聲明了一個(gè)有1000個(gè)成員的數(shù)組books,每個(gè)成員都是自定義類型book的實(shí)例。
struct Books { char title[50]; char author[50]; char subject[100]; int book_id; }; int main(int argc, char const *argv[]) { struct Books books[1000]; books[0].book_id = 22; books[0].book_id = 7; return 0; }
struct的嵌套
struct 結(jié)構(gòu)的成員可以是另一個(gè) struct 結(jié)構(gòu)。
struct species { char* name; int kinds; }; struct fish { char* name; int age; struct species breed; };
上面示例中,fish的屬性breed是另一個(gè) struct 結(jié)構(gòu)species。
// 寫法三 struct fish shark = { .name="shark", .age=9, .breed={"Selachimorpha", 500} };
引用breed屬性的內(nèi)部屬性,要使用兩次點(diǎn)運(yùn)算符(shark.breed.name)。
對(duì)字符數(shù)組屬性賦值,要使用strcpy()函數(shù),不能直接賦值,因?yàn)橹苯痈牡糇址麛?shù)組名的地址會(huì)報(bào)錯(cuò)。
strcpy(shark.breed.name), "Harry");
struct 結(jié)構(gòu)內(nèi)部不僅可以引用其他結(jié)構(gòu),還可以自我引用,即結(jié)構(gòu)內(nèi)部引用當(dāng)前結(jié)構(gòu)。比如,鏈表結(jié)構(gòu)的節(jié)點(diǎn)就可以寫成下面這樣。
struct node { int data; struct node* next; };
上面示例中,node結(jié)構(gòu)的next屬性,就是指向另一個(gè)node實(shí)例的指針。下面,使用這個(gè)結(jié)構(gòu)自定義一個(gè)數(shù)據(jù)鏈表。
// p9-2.c #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char const *argv[]) { struct node { int data; struct node *next; }; struct node *head; // 生成一個(gè)三個(gè)節(jié)點(diǎn)的列表 (11)->(22)->(33) head = malloc(sizeof(struct node)); head->data = 11; head->next = malloc(sizeof(struct node)); head->next->data = 22; head->next->next = malloc(sizeof(struct node)); head->next->next->data = 33; head->next->next->next = NULL; // 遍歷這個(gè)列表 for (struct node *cur = head; cur != NULL; cur = cur->next) { printf("%d\n", cur->data); } return 0; }
實(shí)驗(yàn)
考慮下面發(fā)這些聲明和數(shù)據(jù),并debug
#include <stdio.h> int main(int argc, char const *argv[]) { struct NODE { int a; struct NODE *b; struct NODE *c; }; // 5個(gè)成員的數(shù)組nodes struct NODE nodes[5] = { {5, nodes + 3, NULL}, {15, nodes + 4, nodes + 3}, {22, NULL, NULL}, {12, nodes + 1, nodes}, {18, nodes + 2, nodes + 1}, }; struct NODE *np = nodes + 2; struct NODE **npp = &nodes[1].b; /* nodes 是數(shù)組,數(shù)組名就是該數(shù)組的指針,也是該數(shù)組第一個(gè)元素的指針 */ // 輸出該數(shù)組的地址 printf("nodes的地址是 %p\n", nodes); // 0x7ffeefbff460 printf("nodes的地址是 %p \n", &nodes); printf("nodes+2的地址是 %p \n", nodes + 2); // printf("%d\n", nodes.a)//錯(cuò)誤;指針訪問屬性 需要使用 -> printf("nodes[0] a的值 %d\n", nodes->a); // 5 通過指針訪問 printf("(*nodes).a)的值 %d\n", (*nodes).a); // (*nodes)獲取的是nodes[1] printf("nodes[3] a的值 %d\n", nodes[3].a); // 12 printf("nodes[3].c的值 %p\n", nodes[3].c); //0x7ffeefbff460 // 訪問的是nodes[0] printf("nodes[3].c->a的值 %d\n", nodes[3].c->a); // 5 // printf("%d\n", *nodes); 使用* 操作符對(duì)指針執(zhí)行間接訪問,*nodes的右值是nodes的整個(gè)結(jié)構(gòu) // printf("%d\n", *nodes.a); //錯(cuò)誤 printf("nodes[4]的值地址 %p \n", &nodes[4]); printf("nodes[3].b->b的值 %p \n", nodes[3].b->b); // nodes[3].b 獲取的是 nodes + 1 即 nodes[1]的指針,然后nodes[1]->b ,就是nodes[4]的指針 // [] () . -> 是一級(jí)運(yùn)算 從左往右邊 結(jié)合, * &是二級(jí)運(yùn)算 printf("*nodes[3].b->b的值 %p \n", *nodes[3].b->b); // {18, nodes + 2, nodes + 1},//最后運(yùn)算*,由前邊可以知道 nodes[3].b->b就是nodes[4]的指針,然后*,得到nodes[4]??聪乱惶犷}的驗(yàn)證 printf("nodes[3] a 的值 %d \n", (*nodes[3].b->b).a); // 18 printf("&nodes[3].a 的值%p \n", &nodes[3].a); // 數(shù)子12 第物理地址 // printf("&n??odes[3].c %p \n", &nodes[3].c); printf("&nodes[3].c的值%p \n", &nodes[3].c->a); //數(shù)字15 的物理地址 printf("&nodes->a 的值%p \n", &nodes->a); //數(shù)字5 的物理地址 printf("nodes+2的地址是 %p \n", nodes + 2); printf("np的值%p \n", np); // np為nodes[2]的地址 printf("np->a的值%d \n", np->a); // 12 // printf("np->c->c->a的值%d \n", np->c->c->a); // 12 printf("npp的值%p \n", npp); // np為nodes[2]的地址 // printf("npp->a的值%p \n", npp->a); // 非法 printf("*npp的值%p \n", *npp); printf("**npp的值%p \n", **npp); return 0; }
到此這篇關(guān)于C語言struct結(jié)構(gòu)體介紹的文章就介紹到這了,更多相關(guān)C語言struct內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++可變參數(shù)函數(shù)的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于C++可變參數(shù)函數(shù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12C語言進(jìn)階之字符串查找?guī)旌瘮?shù)詳解
字符串是一種非常重要的數(shù)據(jù)類型,但是C語言不存在顯式的字符串類型,C語言中的字符串都以字符串常量的形式出現(xiàn)或存儲(chǔ)在字符數(shù)組中,下面這篇文章主要給大家介紹了關(guān)于C語言進(jìn)階之字符串查找?guī)旌瘮?shù)的相關(guān)資料,需要的朋友可以參考下2023-01-01關(guān)于C語言多線程pthread庫的相關(guān)函數(shù)說明
下面小編就為大家?guī)硪黄P(guān)于C語言多線程pthread庫的相關(guān)函數(shù)說明。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05C++中實(shí)現(xiàn)fibonacci數(shù)列的幾種方法
本文主要介紹了C++中實(shí)現(xiàn)fibonacci數(shù)列的幾種方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01