语法 †结构体的定义说明了它的组成成员,以及每个成员的数据类型。定义一般形式如下: struct 结构类型名 { 数据类型 成员名 1; 数据类型 成员名 2; ...... 数据类型 成员名 n; }; 内存使用 †struct Record { int i; double x; char str[16]; };
&ref(): File not found: "struct_memory.jpg" at page "+C+结构体"; 定义结构变量的三种方式 †1.先定义结构,再说明结构变量 struct stu { int num; char name[20]; int age; }; struct stu boy1,boy2; 例如: #define STU struct stu STU { int num; char name[20]; int age; }; STU boy1,boy2; 2.在定义结构类型的同时说明结构变量。 struct stu { int num; char name[20]; int age; }boy1,boy2; 3.直接说明结构变量 struct { int num; char name[20]; int age; }boy1,boy2; 结构体嵌套 †struct date { int month; int day; int year; }; struct { int num char name[20]; char sex; struct date birthday; float score; }boy1,boy2; 高级的用法 †struct address { char name[30]; /*姓名,字符数组作为结构体中的成员 */ char street[40]; /*街道*/ unsigned long tel; /*电话,无符号长整型作为结构体中的成员 */ unsigned long zip; /*邮政编码*/ }student[3]={ {"Zhang","Road NO.1",111111,4444}, {"Wang"," Road NO.2",222222,5555}, {"Li"," Road NO.3",333333,6666} } 示例 †#include<stdio.h> #include<string.h> struct _INFO { int num char str[256]; }; int main() { struct _INFO A; A.num = 2014; strcpy(A.str,"Welcome to dotcpp.com"); printf("This year is %d %s\n",A.num,A.str); return 0; } memset的使用 †使用memset来初始化,一个结构体 #include<stdio.h> #include <string.h> typedef struct ss { int num; int dir[5][3]; }tent; //tent a; int main() { tent a; memset(&a,0,sizeof(a)); return 0; } ★月額105円~/容量最大30GB/機能満載! ロリポップ!レンタルサーバー ★ コメント: |