总结:
1.结构体指针如何人访问成员
2.结构体数组与指针如何访问某个元素的成员
3.条件编译如何进行条件编译
#import <Foundation/Foundation.h>
//预编译
//宏定义
#define PI 3.1415926//#define 的第一种形式
#define S(a,b) (a*b)//#define 的第二种形式
//注意:1推荐用大写 驼峰命名法 k+PI 或 PI
// 2宏不是变量
//pragma
#pragma mark add函数
void add (int a,int b){
}
#pragma mark sayHi函数
void sayHi(){
}
#pragma mark main 函数
//条件编译
//条件编译的第一种形式
#ifdef PI
int a = 10;
#else
int a = 20;
#endif
//条件编译的第二种形式
#ifndef wangnima
//没有定义宏定义过,执行代码段一
int b = 30;
#else
int b = 40;
#endif
//条件编译的第三种形式
#if 1
int c = 50;
#else
int c = 60;
#endif
int main(int argc, const char * argv[]) {
printf("%d ",a);
//宏定义
#define PI 3.1415926//#define 的第一种形式
#define S(a,b) (a*b)//#define 的第二种形式
//注意:1推荐用大写 驼峰命名法 k+PI 或 PI
// 2宏不是变量
//pragma
#pragma mark add函数
void add (int a,int b){
}
#pragma mark sayHi函数
void sayHi(){
}
#pragma mark main 函数
//条件编译
//条件编译的第一种形式
#ifdef PI
int a = 10;
#else
int a = 20;
#endif
//条件编译的第二种形式
#ifndef wangnima
//没有定义宏定义过,执行代码段一
int b = 30;
#else
int b = 40;
#endif
//条件编译的第三种形式
#if 1
int c = 50;
#else
int c = 60;
#endif
int main(int argc, const char * argv[]) {
printf("%d ",a);
printf("%d
",b);
printf("%d
",c);
return 0;
}
#import <Foundation/Foundation.h>
typedef struct CPoint{
float X;
float y;
}CPoint;
typedef struct student{
char name[20];
char gender;
float score;
}Student;
typedef struct person{
char name[20];
int age;
}person;
//打印person的name
void printName(person p){
printf("%s ",p.name);
}
//打印数组的成员的name
void printArrName(person arr[],int n){//(person *arr[],int n)
for (int i = 0; i < n; i++) {
printf("%s ",(arr + i)->name);
}
}
void scoreName(Student arr[],int n){
Student *p = arr;
for (int i = 0; i < n; i++) {
if ((p+i)->gender == 'm') {
(p+i)->score += 10;
if ((p+i)->score > 100) {
(p+i)->score = 100;
}
}
}
for (int i = 0; i < n; i++) {
printf("%s %c % f ",(p+i)->name,(p+i)->gender,(p+i)->score);
}
}
void ScoreName(Student *p,int n){
for (int i = 0; i < n; i++) {
if ((p+i)->gender == 'm') {
(p+i)->score += 10;
if ((p+i)->score > 100) {
(p+i)->score = 100;
}
}
}
for (int i = 0; i < n; i++) {
printf("%s %c % f ",(p+i)->name,(p+i)->gender,(p+i)->score);
}
typedef struct CPoint{
float X;
float y;
}CPoint;
typedef struct student{
char name[20];
char gender;
float score;
}Student;
typedef struct person{
char name[20];
int age;
}person;
//打印person的name
void printName(person p){
printf("%s ",p.name);
}
//打印数组的成员的name
void printArrName(person arr[],int n){//(person *arr[],int n)
for (int i = 0; i < n; i++) {
printf("%s ",(arr + i)->name);
}
}
void scoreName(Student arr[],int n){
Student *p = arr;
for (int i = 0; i < n; i++) {
if ((p+i)->gender == 'm') {
(p+i)->score += 10;
if ((p+i)->score > 100) {
(p+i)->score = 100;
}
}
}
for (int i = 0; i < n; i++) {
printf("%s %c % f ",(p+i)->name,(p+i)->gender,(p+i)->score);
}
}
void ScoreName(Student *p,int n){
for (int i = 0; i < n; i++) {
if ((p+i)->gender == 'm') {
(p+i)->score += 10;
if ((p+i)->score > 100) {
(p+i)->score = 100;
}
}
}
for (int i = 0; i < n; i++) {
printf("%s %c % f ",(p+i)->name,(p+i)->gender,(p+i)->score);
}
}
int main(int argc, const char * argv[]) {
Student students[4] = {{"tangmaru",'m',91},{"wangnima",'m',96},{"ruhua",'f',80},{"zhangquandan",'m',90}};
Student *ps = students;
for (int i = 0; i < 4; i++) {
if ((ps+i)->gender == 'm') {
(ps+i)->score += 10;
if ((ps+i)->score > 100) {
(ps+i)->score = 100;
} } }
for (int j = 0; j < 4; j++) {
printf("%s %c %.2f
",(ps+j)->name,(ps+j)->gender,(ps+j)->score);}
Student students[4] = {{"liubei",'m',98},{"zhangfei",'m',97},{"gaunyu",'m',98},{"zhaoyun",'m',99}};
scoreName(students, 4);
ScoreName(students, 4);
// person p1 = {"wangnima",16};
// person p2 = {"tangmaru",18};
// person p3 = {"ruhua",20};
// person arr[3] = {p1,p1,p3};
// printArrName(arr,3);
// printName(p1);
//函数操作结构体数组时需要传入两个参数
//1数组元素的首地址(结构体数组名)2结构体数组元素的个数
// person p3 = {"ruhua",20};
// person arr[3] = {p1,p1,p3};
// printArrName(arr,3);
// printName(p1);
//函数操作结构体数组时需要传入两个参数
//1数组元素的首地址(结构体数组名)2结构体数组元素的个数
//结构体指针
// CPoint arr = {10,20};
// CPoint *p1= &arr;
// p1 = &arr;
// // arr.X 用点取出结构体变量的成员变量
// printf("%.2f ",(*p1).X);
// CPoint *p1= &arr;
// p1 = &arr;
// // arr.X 用点取出结构体变量的成员变量
// printf("%.2f ",(*p1).X);
//指针取结构体变量的成员变量 用*取值,然后再.
//练习:定义一个学生的结构体,里面有两个成员变量 1 姓名 2 年龄 用指针打印成员变量
// Student student = {"袁新峰",78};
// Student student1 = {"刘振艺",18};
// Student student2 = {"王明伟",17};
// Student student3 = {"赵成浩",16};
// Student *ps = &student,*ps1 = &student1,*ps2 = &student2,*ps3 = &student3;
// ps = &student;
// ps1 = &student1;
// ps2 = &student2;
// ps3 = &student3;
// printf("%s %d ",(*ps).name,(*ps).age);
// printf("%s %d ",(*ps1).name,(*ps1).age);
// printf("%s %d ",(*ps2).name,(*ps2).age);
// printf("%s %d ",(*ps3).name,(*ps3).age);
// printf("%s %d ",ps->name,ps->age);
//结构体指针访问成员变量,还可以用箭头这个符号
// CPoint num1 = {10,30};
// CPoint num2 = {10,5};
// CPoint *pn1 = &num1;
// CPoint *pn2 = &num2;
// pn1 = &num1;
// pn2 = &num2;
// float num = 0;
// num = sqrtf(((pn1->X-pn2->X)*(pn1->X-pn2->X))+((pn1->y-pn2->y)*(pn1->y-pn2->y)));
// printf("%.2f ",num);
// Student stu = {1, "lan ou",'m',89};
// Student *ps = &stu;
// ps = &stu;
//// ps->name[0] -= 32;
// ps->name[0] ^= 32;//大小写互相转换用^=32
//// ps->name[0]= 'L';
//// char a1 = ps->name[0] - 32;
//// ps->name[0] = a1;
// printf("%s ",ps->name);
// //结构体数组数组名就是数组首元素的地址 也就是这个结构体数组的指针
// Student students[5] = {{1, "lan ou",'m',89},{2, "lan ou",'m',89},{3, "lan ou",'m',89},{4, "lan ou",'m',89},{5, "lan ou",'m',89}};
// Student *pst = students;
// Student student = {"袁新峰",78};
// Student student1 = {"刘振艺",18};
// Student student2 = {"王明伟",17};
// Student student3 = {"赵成浩",16};
// Student *ps = &student,*ps1 = &student1,*ps2 = &student2,*ps3 = &student3;
// ps = &student;
// ps1 = &student1;
// ps2 = &student2;
// ps3 = &student3;
// printf("%s %d ",(*ps).name,(*ps).age);
// printf("%s %d ",(*ps1).name,(*ps1).age);
// printf("%s %d ",(*ps2).name,(*ps2).age);
// printf("%s %d ",(*ps3).name,(*ps3).age);
// printf("%s %d ",ps->name,ps->age);
//结构体指针访问成员变量,还可以用箭头这个符号
// CPoint num1 = {10,30};
// CPoint num2 = {10,5};
// CPoint *pn1 = &num1;
// CPoint *pn2 = &num2;
// pn1 = &num1;
// pn2 = &num2;
// float num = 0;
// num = sqrtf(((pn1->X-pn2->X)*(pn1->X-pn2->X))+((pn1->y-pn2->y)*(pn1->y-pn2->y)));
// printf("%.2f ",num);
// Student stu = {1, "lan ou",'m',89};
// Student *ps = &stu;
// ps = &stu;
//// ps->name[0] -= 32;
// ps->name[0] ^= 32;//大小写互相转换用^=32
//// ps->name[0]= 'L';
//// char a1 = ps->name[0] - 32;
//// ps->name[0] = a1;
// printf("%s ",ps->name);
// //结构体数组数组名就是数组首元素的地址 也就是这个结构体数组的指针
// Student students[5] = {{1, "lan ou",'m',89},{2, "lan ou",'m',89},{3, "lan ou",'m',89},{4, "lan ou",'m',89},{5, "lan ou",'m',89}};
// Student *pst = students;
// students[0]->number;
// pst->number;
// printf("%d
",pst->number);
// printf("%d ",(pst+1)->number);
// printf("%d ",(pst+2)->number);
// printf("%d ",(pst+3)->number);
// printf("%d ",(*(pst+4)).number);
// printf("%d ",(*pst).number);
// //*(p+i)等价于p[i]
// printf("%d ",(pst+1)->number);
// printf("%d ",(pst+2)->number);
// printf("%d ",(pst+3)->number);
// printf("%d ",(*(pst+4)).number);
// printf("%d ",(*pst).number);
// //*(p+i)等价于p[i]
// for (int i = 0; i < 5; i++) {
// printf("%d %s %c %f
",(pst+i)->number,(pst+i)->name,(pst+i)->gender,(pst+i)->score);
// }
// printf("%c ",pst->name[1]);
// //结构体指针也是一个指针,他所占字节只与操作系统有关
// //64位系统占8个字节 32位系统占4个字节
// printf("%c ",pst->name[1]);
// //结构体指针也是一个指针,他所占字节只与操作系统有关
// //64位系统占8个字节 32位系统占4个字节
// printf("%lu
",sizeof(pst));