标签:
struct 结构体类型名称{
成员类型 成员名称
}; //必须加分号
struct Person{
int age;
char sex;
double height;
double weight;
char *name;
}
struct 结构类型名称 结构体变量名称;
struct Person person;
person.age = 10;
person.sex = ‘M‘;
person.weight=20.2;
person.height=1.2;
person.name="小明";
printf("age=%d,sex=%c,weight=%.2lf,height=%.2lf,name = %s\n",person.age,person.sex,person.weight,person.height,person.name);
struct Person p1;
p1.age = 10;
p1.name="xx";
p1.height:1.3;
struct Person p1
p1 = (struct Person){20,"小明",2.0}
struct Person p2 = {15,"小红",1.5}
p1 = p2;
void test(){
struct Person{
int age;
double height;
};
struct Person p1;
}
void test1(){
struct Person{
int age;
double height;
}p1,p2={20,1.75};
p1 = (struct Person){10,1.2};
}
truct Person{
int age;
double height;
}p1,p2={20,1.75};
int main(){
struct Person{
int age;
};
struct Person p={10};
printf("age=%d\n",p.age );
{
struct Person p={20};
struct Monkey{
char *name;
};
}
struct Monkey m;
}
#include <stdio.h>
struct Person{
int age;
char sex;
};
int main(){
struct Person p2;
p2.sex = ‘s‘;
p2.age = 10;
return 0;
}
#include <stdio.h>
int main(){
struct Date{
int year;
int month;
int day;
};
struct Date date;
printf("%p\n",&date);
printf("%p\n",&date.year);
}
#include <stdio.h>
int main(){
struct Date{
int year;
int month;
int day;
};
struct Date date;
printf("%p\n",&date);
printf("%p\n",&date.year);
size_t size = sizeof(struct Date);
printf("%lu\n",size);
}
struct Person{
int age;
char sex;
};
size_t size2 = sizeof(struct Person);
printf("%lu\n",size2);
#include <stdio.h>
#pragma pack(3)
int main(){
结构类型类型 数组名称[元素个数];
//一般情况下,结构体类型一般定义在函数外部
struct Dog{
char *name;
int age;
};
int main(){
//定义,结构体如果没有初始化,它里面都是垃圾值
struct Dog dogs[5];
//初始化
dogs[0](struct Dog){"旺财",1};
dogs[1].age=1;
dogs[1].name="大黄";
return 0;
}
struct Dog dogs[10] = {{"旺财",1},{"大黄",2},{"来福",3},{"小黑",4},{"小白",5}};
int len = sizeof(dogs)/sizeof(struct Dog);
指针所指数据类型 * 指针变量名称
struct Student{
char *name;
int no;
double score;
};
int main(){
struct Student stu = {"小黑",60,59};
//定义结构体指针
struct Student *sp1;
//赋值
sp1 = &stu;
//定义的同时进行赋值
struct Student *sp2 = &stu;
return 0;
}
(*sp1).score = 60;
sp2->name = "大白";
sp2->no = 59;
//定义一个日期的结构体
struct Date{
int year;
int month;
int day;
};
//定义员工结构体,其中包括日期结构
struct Employee{
int num;
char *name;
struct Date birthday; //这就是结构体嵌套
};
struct Employee emp = {1,"八戒",{2015,9,8}};
struct Person {
char * name
struct Person son;//这是不允许的
};
struct Person{
char *name;
struct Person *son; //这是可以的,可以嵌套自己类型的指针
};
//结构体作为函数参数
void printPerson(struct Person person){
printf("name=%s,age=%d,height=%.2f\n",person.name,person.age,person.height);
}
int main(){
struct Person p = {"小孩",1,0.2};
printPerson(p);
}
//结构体作为函数参数
void printPerson(struct Person person){
printf("name=%s,age=%d,height=%.2f\n",person.name,person.age,person.height);
}
void changePerson(struct Person person){
person.age = 30;
}
int main(){
struct Person p = {"晋三",1,0.2};
printPerson(p);
changePerson(p);
printPerson(p);
}
//结构体作为函数参数
void printPerson(struct Person person){
printf("name=%s,age=%d,height=%.2f\n",person.name,person.age,person.height);
}
void changePerson2(struct Person *person){
person.age = 30;
}
int main(){
struct Person p = {"晋三",1,0.2};
printPerson(p);
changePerson2(&p);
printPerson(p);
}
//写一个函数遍历结构体数组
void printPersons(struct Person *persons,int len){
for(int i=0;i<len;i++){
printf("name=%s,age=%d,height=%.2f\n",persons[i].name,persons[i].age,persons[i].height);
}
}
//更新
void changePersons(struct Person *persons,int len){
//省略...
}
int main(){
struct Person ps[3]={{"小泉",2,0.3},{"晋三",2,0.3}};
int len = sizeof(ps)/sizeof(struct Person);
printPersons(ps,len);
changePersons(ps,len);
printfPersons(ps,len);
}
标签:
原文地址:http://www.cnblogs.com/mentorStudio/p/4792457.html