C8_指针
来源:互联网 发布:沈阳网络电玩城 编辑:程序博客网 时间:2024/06/05 01:16
//
// MyFunction.h
// C8_指针
//
// Created by dllo on 15/7/9.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
// 声明一个结构体
typedef struct student{
char stuName[20];
float chinese;
float english;
float math;
char stuNum[20];// 学号
}Student;
// 1.找到5个人各个学科的平均值
void avgScore(Student stu[],int count);
// 2.找到两门以上不及格的人,并且打印对应的人成绩和学号
void printNoPassStu(Student stu[],int count);
///*************************************************************
// 模拟一次投票过程,对4个人进行投票,投票之后对这4个人,根据票数从小到大进行排序
// 姓名 票数
struct person{
char personName[20];
int num; // 票数
};
typedefstruct person Person;
// 投票
// 参与投票的人数
void vote(Person per[],int perNum);
///**************************************************************
void test(int *a);
///**************************************************************
// 交换a和b的值,通过指针
void exchange(int *p1,int *p2);
///*************************************************************
int test1();
int test2();
//
// MyFunction.m
// C8_指针
//
// Created by dllo on 15/7/9.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import "MyFunction.h"
void avgScore(Student stu[],int count){
float chineseCount = 0;
float englishCount = 0;
float mathCount = 0;
for (int i =0; i < count ; i++) {
chineseCount += stu[i].chinese;
englishCount += stu[i].english;
mathCount += stu[i].math;
}
printf("%.2f\n",chineseCount / count);
printf("%.2f\n",englishCount / count);
printf("%.2f\n",mathCount / count);
}
void printNoPassStu(Student stu[],int count){
for (int i =0; i < count ; i++) {
int num[3] = {-1};
int noPass = 0;
// noPass = stu[i].chinese <= 60 ? ++noPass : noPass;
// noPass = stu[i].english <= 60 ? ++noPass : noPass;
// noPass = stu[i].math <= 60 ? ++noPass : noPass;
// 各科及格人数
if (stu[i].chinese < 60) {
noPass++;
num[0] = stu[i].chinese;
} if (stu[i].english < 60){
noPass++;
num[1] = stu[i].english;
}if (stu[i].math < 60) {
noPass++;
num[2] = stu[i].math;
}
if (noPass >= 2) {
for (int j =0; j < 3; j++) {
if (num[j] >= 0) {
printf("%d " ,num[j]);
}
}
printf("\n");
}
}
}
///***********************************************************
//void vote(Person per[], int perNum){
// for (int i = 0; i < perNum ; i++) {
// char c = 0;
// scanf("%c", &c);
// switch (c) {
// case 'A':
// per[0].num++;
// break;
// case 'B':
// per[1].num++;
// break;
// case 'C':
// per[2].num++;
// break;
// case 'D':
// per[3].num++;
// break;
// default:
// printf("输入错误,无效\n");
// perNum++;
// break;
// }
// }
//}
///*********************************************************
void test(int *a){
printf(".m里的a的地址:%p\n", a);
// a += 20;
*a +=20;
//形参和实参最好保持一致方便提示自己需要做什么
}
///*****************************************************
//只要通过地址操作一般不用写返回值
void exchange(int *p1,int *p2){
int temp = 0;
temp = *p1;
*p1 = *p2;
*p2 = temp;
//交换的是地址对应的内容,不是地址
}
///**************************************************************
int test1(){
int g = 10;
return g;
}
int test2(){
int g;
return g;
}
//
// main.m
// C8_指针
//
// Created by dllo on 15/7/9.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "MyFunction.h"
int main(int argc,const char * argv[]) {
// Student stu1 = { "zhangsan", 98, 82, 65, "11"};
// Student stu2 = { "lisi", 75, 84, 88,"12"};
// Student stu3 = { "wangwu", 78, 83, 66,"13"};
// Student stu4 = { "yangliu", 92, 32, 42,"14"};
// Student stu5 = { "aoteman", 58, 43, 69, "15"};
// Student stu[5] = {stu1, stu2, stu3, stu4, stu5};
// avgScore(stu, 5);
// printNoPassStu(stu, 5);
///************************************************************
// Person per1 = {"zhangsan", 0};
// Person per2 = {"lisi", 0};
// Person per3 = {"wangwu", 0};
// Person per4 = {"yangliu", 0};
// Person per[4] = {per1, per2, per3, per4};
// vote(per, 3);
// for (int i = 0; i < 4; i++) {
// printf("%d ",per[i].num);
// }
// // printf("%d\n",per[0].num);
///***********************************************************
// 1.直接访问:直接通过变量名进行访问称为直接访问
// 2.匿名(间接)访问:通过门牌号,地址,一样可以访问到对应的数据,如:指针
//栈区保存变量,堆区大多是指针,全局静态区保存全局变量 static等 ,常量区存放各种常量的 int a = 10相当于把常量区的10拷贝一份到栈区中
// int a = 10;
// // 取址符 &
// printf("%p\n",&a);
//
// // 定义一个整形的指针变量
// int *p = NULL;
// // 定义指针的方式有4种int*p,int *p, int * p,int* p;在苹果中所使用的为int *p;
// /// int *是类型,p是变量名,NULL是初始值
// printf("%ld\n",sizeof(int *));
// /// int *在32位系统占4个字节,在64位系统占8个字节
//
// p = &a;
// printf("%p\n", p);
//
// // 取值符 *
// printf("%d\n", *p);
// printf("%d\n", *&a);
//
// a += 20;
// printf("%d\n", *p);
// int c = 10 ,b = 5;
// printf("%d\n", c+++b);
// // 先用c的值然后才将c自加1
// printf("%d\n",c);
//
///********************************************************
// int a = 10, b = 20;
// // 通过指针的方式让两个变量值发生交换
/*
*交换了指针所指向的地址里所对应的值
int *pa = &a; // 让pa这个指针指向a所在的位置
int *pb = &b;
int temp = 0;
temp = *pa; // 将pa中的值赋值给temp
*pa = *pb;
*pb = temp;
printf("%d\n%d\n", *pa, *pb);
*/
///通过指针来进行操作,交换的不是指针,而是地址所对应的值
// int *p定义一个指针变量; *p取出p中的值; a * p啊乘以p;
// int *pa = &a;
// int *pb = &b;
/*
*交换了指针的指向,虽然打印出来*pa的值的变化,但是a还是10,并没有修改
int *temp = pa; // 指针重指向
pa = pb;
pb = temp;
printf("%d\n", *pa);
printf("%d\n", a);
*/
//
// int a = 10;
// int *p = &a;
// int **p1 = &p;
// printf("%d\n", **p1); // 通过两次找到a的值
// printf("%p\n", p);
//
//
//
///********************************************************
// int a = 10, b =20, c = 30;
// int arr[3] = {a, b, c };
// for (int i = 0; i < 3 ; i++) {
// printf("%d\n", arr[i]);
// }
//
// printf("%p\n",arr);
// printf("%p\n",&arr[0]);
// printf("%p\n",&a);
// printf("%p\n",&arr[1]);
// arr[0] += 20;
// printf("a = %d, arr[0] = %d\n", a, arr[0]);
///*********************************************************
// int arr[5] = {1, 2, 3, 4, 5};
// int *p = arr;
// printf("%p\n", p);
// printf("%p\n", arr);
// printf("%d\n", *p);
//
// // 指针的算术运算
// int a = 5, b = 10, c = 15, d = 20, e = 25;
// int *pa = &c;
// printf("%p\n", &a);
// printf("%p\n", &b);
// printf("%p\n", &c);
// printf("%p\n", &d);
// printf("%p\n", &e);
// // pa++;
// printf("%p\n", (pa + 2)); // 在地址上加上4个字节
//
// int arr[5] = { 1, 2, 3, 4, 5};
// printf("%p\n", &arr[0]);
// printf("%p\n", &arr[1]);
// printf("%p\n", &arr[2]);
// printf("%d\n", *(arr + 4));
// int *p = arr;
// printf("%d\n", *(p + 3));
// printf("%d\n", p[3]);
// // 语法糖:p[3]作用就是提高代码可读性,并且简化代码
// int arr[5] = {32, 1 , 24, 45, 5};
// int *p = arr;
// for (int i = 0; i < 5 - 1; i++) {
// for (int j = 0; j < 5 - 1 - i; j++) {
// if (*(p + j) > *(p + j + 1)) {
// int temp = 0;
// temp = *(p + j);
// *(p + j) = *(p + j + 1);
// *(p + j + 1) = temp;
// }
// }
// }
// for (int i = 0; i < 5; i++) {
// printf("%d ", *(p + i));
// }
// printf("\n");
//
// // 对指针的算数运算相当于控制指针跳转的方向,++向高位移动,--向低位移动,而类型控制每次跳几个字节,int 4个字节
//
//
//
// int a = 10;
// test(&a);
// printf("主函数a的地址:%p\n", &a);
// printf("%d\n",a);
///***************************************************
// int a = 10, b = 20;
// exchange(&a, &b);
// printf("a = %d\n", a);
// printf("b = %d\n", b);
//
// char str[20] = "65416547632"; // str是一个地址
// char str1[20] = "iPhone";
// strlen(str); // 字符串本身也是指针,字符串本身就带有结束符
// char *p = str1;
// printf("%s\n", str);
// printf("%s\n", p); // 通过判断'\0'来结束
// printf("%c\n", p[1]);
// for (int i = 0; i < strlen(p); i++) {
// printf("%c", p[i]);
// }
// printf("\n");
// 指针,计算字符串长度,strlen
// int len = 0;
// for (int i = 0; p[i] != '\0'; i++) {
// len++;
// }
// printf("%d \n", len);
// int arr[2] = {1, 2};
// // 数组名是第一个元素的首地址,是一个常量的地址
// int *p = arr;
// int a = 10;
// p = &a;
// // 指针的重指向,但是数组不能进行重指向
// char str1[20] = "56523536453asdfaf";
// char str2[20] = "iPhone";
// strcpy(str1, str2);
// for (int i = 0; i < 20; i++) {
//
// printf("%c",str1[i]);
// }
//
// // 有变量一定要先赋初值
// int a = test1();
// int b = test2();
// printf("a = %d, b = %d", a, b);
//空指针,0x0中的值无法修改,指针只对栈和堆作用
int *p = NULL;
printf("%p",p);
int a = 10;
p = &a;
p++; //地址向上移动(看最高级别的变量占几个字节)个字节,向下就用--
p[1];//语法糖
printf("\n");
return 0;
}