//c++
// main.m数组
// C8_指针函数
//spa
// Created by dllo on 15/7/9.指针
// Copyright (c) 2015年 cml. All rights reserved.排序
//字符串
#import <Foundation/Foundation.h>get
#import "MyFunction.h"it
int main(int argc, const char * argv[]) {io
// Student stu1 ={"zhangsan",98,99,99,"8"};
// Student stu2 ={"lisi",61.5,55.5,78,"21"};
// Student stu3 ={"wangwu",57,76,87,"27"};
// Student stu4 ={"maliu",56.5,67,89,"2"};
// Student stu5 ={"fengqi",45,99,100,"3"};
// Student stu[5] ={stu1,stu2,stu3,stu4,stu5};
// 调用取平均值的函数
// avgScore(stu, 5);
// int a =3;
// for (int i =0; i <a; i++) {
// if (i ==1) {
// a++ ;
// }
// printf("%d\n",i);
// }
//
// Person per1 = {"zhangsan",0};
// Person per2 = {"lisi",0};
// Person per3 = {"wangwu",0};
// Person per4 = {"yanglin",0};
// Person per[4]= {per1,per2,per3,per4};
// vote(per, 3);
// for (int i =0; i<4; i++) {
// printf("%d\n",per[i].num);
// }
// 1.直接访问:直接经过变量名进行的访问
// 2.匿名(间接)访问:经过门牌号,地址,同样能够访问到对应的数据,例如指针
// int a =10 ;
//
// // 取址符 &
// printf("%p\n",&a);
//
// // 定义一个整形的指针变量
// int *p = NULL;
// // int * 是类型,p 是变量名,NULL是初始值
// printf("%ld\n",sizeof(int *));
// // 在32位占4个字节,64位占8个字节
//
//// for (int i=0; i<5; i++) {
//// for (int j =0; j<5-i; j++) {
//// if (arr[j]>arr[j+1]) {
//// int temp =0 ;
//// temp =arr[j];
//// arr[j]=arr[j+1];
//// arr[j+1]=temp;
//// }
//// }
//// }
//
// p = &a;
// printf("%p\n",p);
//
// // 取值符 *
// printf("%d\n",*p);
//
// *p +=20;
// printf("%d\n",*p );
//
// int c =10,b =5;
// printf("%d\n",c+++b);// 贪婪法 c和后面的运算符组成最多的有效
// printf("%d\n",++c+b);
// printf("%d\n",c);
// int a =10, b =20;
// 经过指针的方式让两个变量值发生交换
// int * pa = &a;
// int * pb = &b;
// int temp = 0;
// temp = *pa;
// *pa =*pb;
// *pb = temp;
// printf("%d,%d\n",a,b);
/* *****经过指针来进行的操做,交换的是地址所对应的值,而不是指针
交换了指针的指向,随让打印出来的*pa的值变化,可是a仍是10,没有修改
int *temp = pa;
pa =pb;
pb =temp;
printf("%d,%d\n",*pa,*pb);
*/
// int a =10;
// int *p =&a;
// int **p1 = &p;
// int ***p2 =&p1;
// printf("%p\n",**p2);
// printf("%p\n",*p1);
// 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("%d\n",&a);
//
// printf("a = %d,arr[0] = %d\n",a,arr[0]+20);
// 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);
// printf("%p\n",pa+2);
// 语法糖:做用:就是提升代码的可读性,而且简化代码
// int arr[5]={1,2,3,4,5};
// printf("%p\n",&arr[0]);
// printf("%d\n",arr[3]);
// int *p =arr;
// printf("%d\n",p[3]);// p[3] =*(p+3)
// 语法糖:做用:就是提升代码的可读性,而且简化代码
// for (int i =0; i <5; i++) {
// for (int j =0; j<5-i; j++) {
// if (*(p+j)>*(p+j+1)) {
// int temp =0;
// temp = *(p+j);
// *(p+j)=*(p+1+j);
// *(p+j+1)=temp;
// }
// }
// }
// 对指针的运算至关于龙之指针跳转的方向,++向高位移动,--向低位移动,而类型控制每次跳几个字节,int 4个字节,short 2个字节
// int b =10;
// test(&b);
// printf("主函数a的地址:%p\n",&b );
// printf("%d\n",b);
//
// int a=10,b=20;
// exchange(&a,&b);
// printf("a=%d\nb=%d\n",a,b);
// char str[20] = "12121";
// char *p =&str[0];
// printf("%s\n",p);
// printf("%c\n",p[1]);
// for (int i =0; i<strlen(p); i++) {// strlen(首地址,遇到\0结束)
// printf("%c",p[i]);
// }
// printf("\n");
// 指针,计算字符串的长度,strlen
// char str[20] ="char";
// char *p =str ;
// printf("%d\n",strlen(p));
// char str[20] ="char";
// char *p =str;
// 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] = "12326sdsd99";
// char str2[20] = "iPone";
// strcpy(str1, str2);
// for (int i =0; i<20; i++) {
// printf("%c",str1[i]);
// }
// printf("\n%s\n",str1);
// 右边梁必定要赋值
// int a =test1();
// int b = test2();
// printf("a=%d,b =%d\n;",a,b);
//
// 空指针
// int *p =NULL;
// printf("%p\n",p);
//
// int a=10;
// p = &a;
// p++;
// p[1] =arr[1];
return 0;
}
.m文件
//
// MyFunction.m
// C8_指针
//
// Created by dllo on 15/7/9.
// Copyright (c) 2015年 cml. All rights reserved.
#import "MyFunction.h"
void avgScore(Student stu[],int count){
float chineseCount = 0;
for (int i =0; i <count; i++) {
chineseCount +=stu[i].chinese;
}
printf("%.2f\n",chineseCount/count);
}
void printNoPassStu(Student stu[],int count){
for (int i =0; i <count; i++) {
//
int num[3] ={-1,-1,-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<60;
}
if (stu[i].math) {
noPass++;
num[2]=stu[i].math;
}
if (noPass>=2) {
for (int i=0; i<3; i++) {
if (num[i]>=0) {
printf("%d\n",num[i]);
}
}
}
}
}
void vote(Person per[],int perNum){
for (int i =0; i < perNum; i++) {
char c = 0;
scanf("%c",&c);
getchar();
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 和b的值,经过指针
void exchange(int *a,int *b){
int temp=0;
temp =*a;
*a =*b;
*b =temp;
}
int test1(){
int g =10;
return g;
}
int test2(){
int g;
return g;
}
// MyFunction.h
// C8_指针
// Created by dllo on 15/7/9.
// Copyright (c) 2015年 cml. All rights reserved.
#import <Foundation/Foundation.h>
// 声明一个结构体
typedef struct student{
char stuName[20];
float chinese;
float english;
float math;
char stuNum[20]; // 学号
}Student;
// 1.找到五我的各个学科的平均值
void avgScore(Student stu[],int count);
// 2.找到两门学科以上不及格的人数,而且打印对应的人的成绩和学号
void printNoPassStu(Student stu[],int count);
// 模拟一个投票过程,对4我的进行投票,投票以后对4我的,根据票数从小到大排序
// 姓名 票数
struct person{
char personName[20];
int num ; //票数
};
typedef struct person Person;
// 投票过程
//struct person{
// char personName;
//};
void vote(Person per[],int perNum);
void test(int *a);
// 交换a 和b的值,经过指针
void exchange(int *a,int *b);
int test1();
int test2();