//
// structandpointer.c
// IOS150608
//
// Created by Peng Junlong on 15/6/8.
// Copyright (c) 2015年 Peng Junlong. All rights reserved.
//
#include "structandpointer.h"
#include <string.h>
//******************************
//* *
//* struct定义结构体的关键字 *
//* *
//******************************
struct Student
{
char name[20];
int age;
int score;
};
//使用"."运算符访问结构体变量的成员
//void sort_student(struct Student stu[],int stuCount);
//int main(int arg, const char *argv[])
//{
//
// struct Student student1 = {"张三",23,88};
// printf("name = %s,age = %d,score = %d\n",student1.name,student1.age,student1.score);
//
// struct Student stu[100] = {};
// strcpy(stu[0].name, "李四");
// stu[0].age = 33;
// stu[0].score = 90;
// printf("name = %s,age = %d,score = %d\n",stu[0].name,stu[0].age,stu[0].score);
// for (int i=1; i<5; i++) {
// scanf("%s%d%d",stu[i].name,&stu[i].age,&stu[i].score);
// }
// printf("\n");
// sort_student(stu,5);
// for (int i=0; i<5; i++) {
// printf("%s %d %d \n",stu[i].name,stu[i].age,stu[i].score);
// }
// return 0;
//}
//
//void sort_student(struct Student stu[],int stuCount)
//{
// struct Student tempStudent;
// tempStudent = stu[0];
// for (int i=0; i<stuCount-1; i++) {
// for (int j=0; j<stuCount-i-1; j++) {
// if (stu[j].score > stu[j+1].score) {
// tempStudent = stu[j];
// stu[j] = stu[j+1];
// stu[j+1] = tempStudent;
// }
// }
// }
//
//}
//利用typedef关键字:
//1.给基本数据类型取名
typedef unsigned int uint_32;
//2.给复杂类型取名
typedef int (* functionPointer)(int, int);
int add(int a, int b)
{
return a+b;
}
//3.给结构体重新取名
//写法1:
//typedef struct StudentInfo
//{
// char name[20];
// int age;
// int score;
//}StudentInfo;
//写法2:
typedef struct
{
char name[20];
int age;
int score;
}StudentInfo;
//结构体指针
//int main(int argc, const char *argv[])
//{
// functionPointer funp = add; //functionPointer <-->int (*)(int ,int);
// printf("aum = %d\n",funp(4,6));
//
// StudentInfo student1 = {"张三",23,88};
// StudentInfo *pstudent = &student1;//结构体指针
// printf("name = %s,age = %d,score = %d\n",student1.name,student1.age,student1.score);
// printf("name = %s,age = %d,score = %d\n",pstudent->name,pstudent->age,pstudent->score);
//
// StudentInfo stu[100] = {"李四",22,88,"李三",23,87,"李一",24,89};
// pstudent = stu;
// for (int i=0; i<3; i++) {
// printf("name = %s,age = %d,score = %d\n",pstudent->name,pstudent->age,pstudent->score);
// pstudent++;
// }
// //printf("name = %s,age = %d,score = %d\n",stu[0].name,stu[0].age,stu[0].score);
// return 0;
//}
//结构体大小:结构体成员内存对齐后的大小之和.从前往后对齐,与成员变量类型大的进行对齐;
//一般将结构体成员数据类型从小到大的顺序排列,保证结构体类型变量占用的内存空间小,因为结构体成员的顺序影响占用内存的大小
//typedef struct
//{
// char cf;
// char name[15];
// int age;
// long f;
//}StructSize;
//
//int main(int argc, const char *argv[])
//{
// printf("%lu\n",sizeof(StructSize));
//
// return 0;
//}
//******************************
//* *
//* union联合体(共同体) *
//* *
//******************************
typedef union
{
int number;
float weight;
}Amount;
//多个变量共用同一块内存,每次使用只是用其中一个
//初始化只能初始化联合体中的第一个,并且只能是一个
//占用内存大小就是联合体中最大的数据类型那个的大小
//int main(int argc, const char *argv[])
//{
// Amount mount;
// printf("%p\n",&mount.number);
// printf("%p\n",&mount.weight);//地址一样
//
// mount.weight = 3.14;
// printf("%f\n",mount.weight);
//
// mount.number = 300;
// printf("%f\n",mount.weight);
//
// return 0;
//}
//typedef struct
//{
// char name[30];
// float price;
// Amount amount;
// struct Date
// {
// int year;
// int month;
// int day;
// }date;
//}Goods;
//
//int main(int argc, const char *argv[])
//{
// printf("%lu\n",sizeof(Amount));
// Goods cup = {"杯具",9.9,{ },{2015,6,8}};
// cup.amount.number = 889;
//
// printf("name = %s,price = %.2f,amount = %d,%d年%d月%d日\n",
// cup.name,cup.price,cup.amount.number,cup.date.year,cup.date.month,cup.date.day);
//
// Goods rice = {"大米",90.9,{},{2015,6,8}};
// rice.amount.weight = 35.5;
//
// printf("name = %s,price = %.2f,amount = %.2f,%d年%d月%d日\n",
// rice.name,rice.price,rice.amount.weight,rice.date.year,rice.date.month,rice.date.day);
//
// return 0;
//}
//******************************
//* *
//* enum枚举 *
//* *
//******************************
//枚举是int类型,和成员没有任何关系,占用的内存空间为4个字节
//枚举的成员是枚举常量,而不是变量,第一个成员默认值是0,可以自行改变默认值
//typedef enum
//{
// MON=1,
// TUE,
// WES,
// THU,
// FRI,
// SAT,
// SUN
//}Weekday;
//
//int main(int argc, const char *argv[])
//{
//
// printf("%d\n",SUN);
// Weekday day;
// scanf("%d",&day);
// switch (day) {
// case MON:
// printf("星期一\n");
// break;
// case TUE:
// printf("星期二\n");
// break;
// case WES:
// printf("星期三\n");
// break;
// case THU:
// printf("星期四\n");
// break;
// case FRI:
// printf("星期五\n");
// break;
// case SAT:
// printf("星期六\n");
// break;
// case SUN:
// printf("星期天\n");
// break;
//
// default:
// break;
// }
//}