C語言的結構體使用

2021-08-10 12:48:23 字數 1867 閱讀 4048

1、基本的建立結構體和使用
#include#include#includestruct man

;void main() ;

struct man man1;

man1.age = 30;

man1.name = "jam";

printf("%d,%s\n", man.age, man.name);

printf("%d,%s\n", man1.age, man1.name);

getchar();

}

2、定義結構體的同時定義結構體變數

#include#include#includestruct man

man1;

void main()

3、定義結構體的同時定義結構體變數並賦值

#include#include#includestruct man

man1, man = ;

void main()

4、匿名結構體,控制結構體變數的個數(限量版),相當於單例

#include#include#includestruct 

w;void main()

5、結構體的巢狀使用

#include#include#includestruct man

;struct boy

;void main()

6、結構體指標的使用

#include#include#includestruct man

;void main()

7、結構體陣列和結構體指標

#include#include#includestruct man

;void main() , };

struct man *p = mans;

for (int i = 0; i < 2; i++)

getchar();

}

陣列大小的演算法:
int size=sizeof(mans) / sizeof(struct man);

printf("%d\n", size);

for (int i = 0; i < size; i++)

//結構體變數的大小,必須是最寬基本資料型別的整數倍,如果結構體同時存在整形-4和雙精度-8,則占用記憶體都是最寬的8.

//提公升讀取的效率

8、結構體與動態記憶體分配

#include#include#includestruct man

;void main()

getchar();

}

9、結構體中的函式指標的使用

#include#include#includestruct man

;void sayhi(char *text)

void main()

#include#include#includestruct man

;void sayhi(char *text)

void rename(struct man *man)

void main()

c語言 結構體的使用

結構體的定義形式為 struct 結構體名 結構體是一種集合,它裡面包含了多個變數或陣列,它們的型別可以相同,也可以不同,每個這樣的變數或陣列都稱為結構體的成員 member 請看下面的乙個例子 struct stu stu 為結構體名,它包含了 5 個成員,分別是 name num age gro...

C語言 結構體的使用

include include define number 5 define name len 64 void swap int int x,int y void swap str char sx,char sy void sort int num,char str name len int n i...

C語言結構體與結構體指標的使用

c語言結構體 struct 是由一系列具有相同型別或不同型別的資料構成的資料集合。說的通俗一點就是乙個集合。c語言是一門面向過程的程式語言,而結構體的使用在某些層次上跟物件導向有點異曲同工之處了。下面回歸正題,學習一下結構體以及結構體指標的知識。一 結構體變數的定義和初始化 1 首先我們來看一下結構...