iOS C语言8_指针

来源:互联网 发布:csv数据怎么转换成mat 编辑:程序博客网 时间:2024/05/22 11:52

.h

//

//  MyFunction.h

//  C8_指针

//

//  Created by dllo on 15/7/9.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

// 声明一个结构体

typedef struct student{

   char stuName[20];

   float chinese;

   float english;

   float math;

   char stuNum[20];

}Student;

//找到五个人各学科的平均值

void avgScore(Student stu[],int count);

// 找到两门以上不及格的人数,并且打印对应的人成绩和学号

void prientNoPssStu(Student stu[],int count);

// 模拟一次投票过程,对四个人进行投票,投票之后对这四个人,根据票数从小到大进行排序

struct person{

   char personName[20];

   int num;        //票数

    

};

typedef struct person  Person;

//投票

void vote(Person per[],int perNum); //参与投票人数

void test(int *a);

//交换ab的值,通过指针

void exchange(int *a,int *b);


int test1();

int test2();














.m

//

//  MyFunction.m

//  C8_指针

//

//  Created by dllo on 15/7/9.

//  Copyright (c) 2015 zhozhicheng. 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 prientNoPssStu(Student stu[],int count){

//    

//    for (int i=0; i<count; i++) {

//        //

//        int num[3]={0};

//        int noPass = 0;

//        if (stu[i].chinese <= 60) {

//            noPass++;

//            num[1]=stu[i].chinese;

//            

//        }if (stu[i].math <= 60) {

//            noPass++;

//            num[2]= stu[i].math;

//        }if (stu[i].english <= 60 ) {

//            noPass++;

//            num[3]=stu[i].english;

//        }

//        if (noPass >= 2) {

//            for (int j=0; j<3; j++) {

//                if (num[j]>=0) {

//                    printf("%f",num[j]);

//                }

//            }

//        }

//        

//        }

//        

//}


        

//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(".ma的地址%p",a);

//    a += 20;

//}

//        

//        

//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 f;

//    return  f;

//}



    

    

    

    






main

//

//  main.m

//  C8_指针

//

//  Created by dllo on 15/7/9.

//  Copyright (c) 2015 zhozhicheng. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "MyFunction.h"

int main(int argc,const char * argv[]) {

//    Student stu1={"zhangsan",78.5,61.5,55,"11"};

//    Student stu2={"lisi",44,89,86,"13"};

//    Student stu3={"wangermazi",55.5,69.5,65,"14"};

//    Student stu4={"shenliu",89,89.5,56,"15"};

//    Student stu5={"tianqi",44,89,56,"16"};

//    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 ={"zhansgan",0};

//    Person per2={"lisi",0};

//    Person per3={"wangwu",0};

//    Person per4={"shenliu",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",&a);

//////    // 定义一个整型指针变量

//    int *p = NULL;

//////    //int *是类型,p是变量名,NULL是初始值

//////    printf("%ld",sizeof(int *));

//////    // 32位占4个字节,648个字节

//    p=&a;

//    printf("%p\n",p);

//    // 取值符

//    printf("%d\n",*&a);

//    *p += 20;

//    printf("%d",*p);

//    

    

//    int c=10,b=5;

//    printf("%d\n",c+++b);

//    printf("%d",c);

    

//    int a = 10,b=20;

//    //通过指针方式让两个变量值发生变换

//    int *pa =&a;

//    int *pb=&b;

//    // 交换了指针的指向,虽然打印出来*pa的值变化,但是a还是10,并没有修改

//    int *temp=pa;

//    pa=pb;

//    pb=temp;

//    

//    int temp=0;

//    temp=*pa;

//    *pa=*pb;

//    *pb=temp;

//    printf("%d\n%d\n",*pa,*pb);

//    // 通过指针进行操作,交换的不是指针,而是地址所对应的值

    

//    int a=10;

//    int *p=&a;

//    int **p1=&p;

//    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[0]);

//    printf("%p\n",&a);

    

    

//    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]={3,2,1,4,5};

//    printf("%p\n",&arr[0]);

//    printf("%p\n",&arr[1]);

//    int *p =arr;

//    printf("%d\n",*(p+3));

//    // 语法糖:作用就是提高代码的可读性,并且简化代码

//    

//    for (int i = 0; i<5-1; i++) {

//        for (int j= 0; j<5-1-j; j++) {

//            if (*(p+j)>*(p+j+1)) {

//                int temp=0;

//                temp = *(p+j);

//                *(p+j)=*(p+j+1);

//                *(p+j+1)=temp;

//            }

//        }

//    }

    //对指针的算术运算相当于控制指针跳转的方向,++向高位移动,--向低位移动,而类形控制每次跳几个字节,int四个字节,

    

    

//    int a=10;

//    test(a);

//    printf("主函数a的地址=%p\n",&a );

//    printf("%d\n",a);

//    int a =19,b=20;

//    exchange(&a,&b);

//    printf("%d\n%d\n",a,b);

//    

//    char str[20]="1298456121";

//    strlen(str);

//    char *p = str;

//    printf("%s\n",p);

//    printf("%c\n",p[2]);

//    for (int i=0; i<strlen(str); i++) {

//        printf("%c\n",p[i]);

//    }

    

    // 指针,计算字符串的长度,strlen

//    char str[20]="chajhggr";

//    char *p=str;

//    

//    int len =0;

//    for (int i=0; p[i] != '\0'; i++) {

//        len++;

//    }

//    printf("%d\n",len);

    

//    int arr[2]={1,2};

    //因为数组的名是第一个元素的首地址,这个地址是一个常量的地址

    

//char str1[20]="1545gfstwef";

//    char str2[20]="iphone";

//    strcpy(str1, str2);

//    for (int  i=0; i<20; i++) {

//        printf("%c",str1[i]);

//        

//    }printf("\n");

    

//    

//    int a =test1();

//    int b=test2();

//    printf("%d\n%d\n",a,b);

    

    //空指针

//    int *p =NULL;

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

   return 0;

}





0 0
原创粉丝点击