構造資料型別簡單介紹

2021-07-16 14:20:38 字數 2685 閱讀 2649

結構體的出現,能夠將不同型別資料組成的組合型的資料結構,結構體型別的一半角式為

struct 結構體名

定義結構體的變數的三種方法:

1.先宣告結構體型別,再定義該型別的變數。

struct student

;       再在主函式中宣告   struct student stu1; stu1就是乙個結構體型別。

2.在宣告型別的同時定義變數。

struct student

a;3.在宣告型別的同時定義變數(結構體名可以省略)typedef 代替乙個複雜的型別

typedef struct data

data;    可以用data來定義。比如: data a a就是乙個結構體型別了。

*///結構體變數的初始化和引用

struct student

;typedef struct data

data;

int main()

; //可以在定義的時候初始化

//printf("%d\n",sizeof(stu)); //計算機在開闢記憶體的時候,採用的是位元組對齊。小的在兩邊的話,採取的是分別對齊,小的在一堆的話,對齊乙個就行了。

/* 引用結構體變數中成員的值的時候,引用方式為: 結構體變數名.成員名 

注意:不能通過輸出結構體變數名來達到輸出結構體變數名所有成員的值。

同種型別的結構體變數可以相互賦值。

*/struct student stu = ;

struct student stu1;

typedef struct student stu;

int i;

stu *p = &stu;

stu a[5] = ,,,

,};//

data a;

// printf("%d\n",sizeof(stu));

printf("%d %s %c\n",stu.number,stu.name,stu.***);

stu.number = 5;

strcpy(stu.name,"asd");//gets(stu.name);

stu.*** = 'm';

printf("%d %s %c\n",stu.number,stu.name,stu.***);

scanf("%d",&stu.number);

getchar();

gets(stu.name);

scanf("%c",&stu.***);

printf("%d %s %c\n",stu.number,stu.name,stu.***);

stu1 = stu;

printf("%d %s %c\n",stu1.number,stu1.name,stu1.***);

for(i = 0;i<5;i++)

printf("%d %s %c\n",a[i].number,a[i].name,a[i].***);

// for(i = 0;i<5;i++)

// scanf("%d %s %c",&a[i].number,a[i].name,&a[i].***);

printf("%d,%d\n",p->number,(*p).number);

p = a;

}#endif

/*結構體陣列 

定義:1.

struct 結構體名

陣列名[陣列長度]

2.先宣告乙個結構體型別

結構體型別 陣列名[陣列長度}

賦值的話可以用迴圈對其賦值,也可以在定義的時候賦值。

結構體指標

定義:1.先定義 struct student *pt 再將p指向乙個結構體變數就可以了

引用:  (*p).成員名  p->成員名  這兩種方法都是可以的。

指向結構體陣列的指標

(1)先宣告乙個結構體型別,並且定義結構體陣列,同時初始化。

(2)定義乙個指向結構體型別的指標變數p;

(3)讓p指向結構體陣列的首元素,依次輸出

*/#if 0

typedef struct student

stu;

void sort(stu *a,int n);

void print(stu *a,int n);

void input(stu *a,int n);

int main()

void input(stu *a,int n)

void sort(stu *a,int n)

}void print(stu *a,int n)

#endif

#if 0 //靜態鍊錶 由於靜態鍊錶的使用不是特別的多,初略的看一下**就行了。

typedef struct node

node;

int main()}}

#endif

#if 0 //動態鍊錶的創立

typedef struct node

node;

node *creat();

void print(node *head);

int main()

node *creat()

free(pnew);

pnew = null;

return head; //將首位址返回

}void print(node *head)

printf("\n");

}#endif

js資料型別簡單介紹

js資料型別 ecmascript中有5種簡單的資料型別 undefined,null,boolean,number,string.還有一種複雜的資料型別 object 本質上是由一組無序的名值對組成的 typeof操作符 用於檢測給定變數的資料型別 undefined 未定義 boolean 布林...

Python學習 資料型別(簡單介紹)

a,b,c,d 66,6.6,false,6 6j print type a type b type c type d class int class float class bool class complex 3 isinstance 函式與type 函式有類似的功能 isinstance nu...

資料型別介紹

資料大小 1 b 1byte 1位元組 8bit bit 電子位,簡稱位 1024位元組 1kb byte 8位 short 16位 int 32位 long 64位 儲存單位最高位表示正負數,1表示負數0表示正數.反碼 原碼每位取反。補碼 反碼 1 2儲存2的補碼 要運算,必須先儲存,要儲存必須要...