C說話中的構造體的入門進修教程。本站提示廣大學習愛好者:(C說話中的構造體的入門進修教程)文章只能為提供參考,不一定能成為您想要的結果。以下是C說話中的構造體的入門進修教程正文
C說話中數組許可界說類型的變量,可包容雷同類型的多個數據項,但構造體在C說話編程中,它許可界說分歧品種的數據項可供其他用戶界說的數據類型。
構造是用來代表一個記載,假定要跟蹤藏書樓的書本。能夠要跟蹤有關每本書以部屬性:
界說構造體
界說一個構造體,必需應用構造體的struct語句。該struct語句界說了一個新的數據類型,法式不止一個成員。struct語句的格局是如許的:
struct [structure tag]
{
member definition;
member definition;
...
member definition;
} [one or more structure variables];
構造體(structure)標簽是可選的,每一個成員的界說是一個正常的變量界說,如 int i; 或 float f; 或任何其他有用的變量的界說。在構造的界說的開頭,最初的分號之前,可以指定一個或多個構造變量,但它是可選的。這裡是聲明書(Book)的構造方法:
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
拜訪構造體成員
要拜訪構造體的任何成員,我們應用成員拜訪運算符(.)成員拜訪運算符是編碼作為構造體變量名,而且願望拜訪構造體部件。應用struct症結字來界說構造體類型的變量。以下為例子來說明構造的用法:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s
", Book1.title);
printf( "Book 1 author : %s
", Book1.author);
printf( "Book 1 subject : %s
", Book1.subject);
printf( "Book 1 book_id : %d
", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s
", Book2.title);
printf( "Book 2 author : %s
", Book2.author);
printf( "Book 2 subject : %s
", Book2.subject);
printf( "Book 2 book_id : %d
", Book2.book_id);
return 0;
}
讓我們編譯和運轉下面的法式,這將發生以下成果:
Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
構造體作為函數參數
可以傳遞一個構造作為函數的參數,異常相似傳遞任何其他變量或指針。拜訪可以象在下面的例子曾經拜訪相似構造變量的方法:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books book );
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printBook( Book1 );
/* Print Book2 info */
printBook( Book2 );
return 0;
}
void printBook( struct Books book )
{
printf( "Book title : %s
", book.title);
printf( "Book author : %s
", book.author);
printf( "Book subject : %s
", book.subject);
printf( "Book book_id : %d
", book.book_id);
}
讓我們編譯和運轉下面的法式,這將發生以下成果:
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
指針構造
異常類似界說指針構造,來界說指向任何其他變量,以下所示:
struct Books *struct_yiibaier;
如今,可以存儲構造變量的地址在下面界說的指針變量。為了找到一個構造變量的地址,將應用運算符&在構造體的名字之前,以下所示:
struct_yiibaier = &Book1;
拜訪應用一個指向構造的構造的成員,必需應用 -> 運算符以下:
struct_yiibaier->title;
讓我們從新寫下面的例子中應用構造指針,願望這將可以或許讓我們更輕易地輿解概念:
#include <stdio.h>
#include <string.h>
struct Books
{
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
int main( )
{
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 );
/* print Book2 info by passing address of Book2 */
printBook( &Book2 );
return 0;
}
void printBook( struct Books *book )
{
printf( "Book title : %s
", book->title);
printf( "Book author : %s
", book->author);
printf( "Book subject : %s
", book->subject);
printf( "Book book_id : %d
", book->book_id);
}
讓我們編譯和運轉下面的法式,這將發生以下成果:
Book title : C Programming Book author : Nuha Ali Book subject : C Programming Tutorial Book book_id : 6495407 Book title : Telecom Billing Book author : Zara Ali Book subject : Telecom Billing Tutorial Book book_id : 6495700
位字段
位字段許可數據在一個構造體包裝。這是特殊有效的,當內存或存儲數據異常名貴。典范的例子:
包裝幾個對象到一個機械說話。例如1位標記可以或許緊縮長度
讀取內部的文件格局 - 非尺度的文件格局可以讀出。例如: 9位整數。
C說話許可我們經由過程構造界說:bit 長度的變量以後。例如:
struct packed_struct {
unsigned int f1:1;
unsigned int f2:1;
unsigned int f3:1;
unsigned int f4:1;
unsigned int type:4;
unsigned int my_int:9;
} pack;
在這裡,packed_struct包括6個成員:四個1位標記s f1..f3, 一個 4 位類型和9位my_int。
C說話主動包裝上述位字段盡量緊湊,前提是字段的最年夜長度小於或等於盤算機的整數字長。假如不是這類情形,那末一些編譯器可以許可,而其他將堆疊存儲鄙人一個字段的存儲器。
指針和數組:
這是永久繞不開的話題,起首是援用:
struct stuff *ref = &Huqinwei;
ref->age = 100;
printf("age is:%d\n",Huqinwei.age);
打印可見變更
指針也是一樣的
struct stuff *ptr;
ptr->age = 200;
printf("age is:%d\n",Huqinwei.age);
構造體也不克不及免俗,必需稀有組:
struct test{
int a[3];
int b;
};
//關於數組和變量同時存在的情形,有以下界說辦法:
struct test student[3] = {{{66,77,55},0},
{{44,65,33},0},
{{46,99,77},0}};
//特殊的,可以簡化成:
struct test student[3] = {{66,77,55,0},
{44,65,33,0},
{46,99,77,0}};
變長構造體:
可以變長的數組
#include <stdio.h>
#include <malloc.h>
#include <string.h>
typedef struct changeable{
int iCnt;
char pc[0];
}schangeable;
main(){
printf("size of struct changeable : %d\n",sizeof(schangeable));
schangeable *pchangeable = (schangeable *)malloc(sizeof(schangeable) + 10*sizeof(char));
printf("size of pchangeable : %d\n",sizeof(pchangeable));
schangeable *pchangeable2 = (schangeable *)malloc(sizeof(schangeable) + 20*sizeof(char));
pchangeable2->iCnt = 20;
printf("pchangeable2->iCnt : %d\n",pchangeable2->iCnt);
strncpy(pchangeable2->pc,"hello world",11);
printf("%s\n",pchangeable2->pc);
printf("size of pchangeable2 : %d\n",sizeof(pchangeable2));
}
運轉成果
size of struct changeable : 4 size of pchangeable : 4 pchangeable2->iCnt : 20 hello world size of pchangeable2 : 4
構造體自己長度就是一個int長度(這個int值平日只為了表現後邊的數組長度),後邊的數組長度不盤算在內,然則該數組可以直接應用。
(說後邊是個指針吧?指針也占長度!這個是不占的!道理很簡略,這個器械完整是數組後邊的尾巴,malloc開拓的是一片持續空間。其實這不該該算一個機制,感到應當更像一個技能吧)
構造體嵌套:
構造體嵌套其實沒有太不測的器械,只需遵守必定紀律便可:
//關於“一錘子生意”,只對終究的構造體變量感興致,個中A、B也可刪,不外最好帶著
struct A{
struct B{
int c;
}
b;
}
a;
//應用以下方法拜訪:
a.b.c = 10;
特殊的,可以一邊界說構造體B,一邊就應用上:
struct A{
struct B{
int c;
}b;
struct B sb;
}a;
應用辦法與測試:
a.b.c = 11;
printf("%d\n",a.b.c);
a.sb.c = 22;
printf("%d\n",a.sb.c);
成果無誤。