c_指针

来源:互联网 发布:耐克淘宝旗舰店靠谱不 编辑:程序博客网 时间:2024/05/30 02:22

(1)main.m

//

//  main.m

//  c8_指针

//

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

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

//


#import <Foundation/Foundation.h>

#import "MyFunction.h"

int main(int argc,constchar * argv[]) {

////模拟投票过程,4人进行投票,投票后,根据票数对4人从小到大进行排序

//    int a=3;

//    for (int i=1; i<a; i++) {

//        if (i==1) {

//            a++;

//        }

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

//    }

//person per1={"anyichen",0};

//person per2={"ningzhiyuan",0};

//person per3={"xiaoyahuizi",0};

//person per4={"anleyan",0};

//person per[4]={per1,per2,per3,per4};

//vote(per,3);

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

//        printf("%d ",per[i].num);

//    }

////指针**********************************************

//1.直接访问:直接通过变量名进行访问,称为直接访问

//2.匿名(间接)访问:通过门牌号,地址,一样可以访问到对应的数据,例如指针


//int a=10;

//取址符

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

//定义一个整形的指针变量

//int *p=NULL;

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

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

//int * 32位占4个字节,64位占8个字节

// 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); //先用后加1,++c先加1再使用

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


    int a=10,b=20;

    ////交换地址内容,交换的不是指针,而是地址对应的值**********************************

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

//    int *pa=&a;

//    int *pb=&b;

//    int temp=0;

//    temp=*pa;////交换的是值,pa是指针,*pa为取值

//    *pa=*pb;

//    *pb=temp;

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

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

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

    int *pa=&a;

    int *pb=&b;

    int *temp;

    temp=pa;////交换的是指针的指向,int *temp定义了temp是指针,进行指针交换,交换指向

    pa=pb;

    pb=temp;

    printf("a=%d\n",*pa);

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

//    int a=10;

//    int *p=&a;

//    int **p1=&p;

//    printf("%d\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);

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

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

//    arr[0]+=20;

//    printf("a=%d,arr[0]=%d\n",a,arr[0]);

//


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

//    int *p=arr;//p是指针,而不是*p;int *是指针的变量类型

//    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]={32,1,24,45,5};

//    int *p=arr;         //*p=&arr[0]

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

//    printf("%d\n",*(arr+4));

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

//    printf("%d\n",p[3]);//arrp都是表示首地址.所以arr[i]p[i]表示相同

////语法糖:提高代码可读性,并简化代码.************************************

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

//            }

//        }

//    }

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

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

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

//    }printf("\n");


//    int a=10;

//    test(&a);

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

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

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

//    int a=10,b=15;

//    exchange(&a,&b);

//    printf("a=%d b=%d",a,b);

//    printf("\n");

////

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

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

//    char *p=str;

//    printf("%c\n",*(p+4));

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

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

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

//    //strlen(str);

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

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

//    }printf("\n");

//通过指针计算字符串长度

//    int len=0;

//    for (int j=0; *(p+j)!='\0'; j++) {//可用p[j]!='\0';

//        //if (*(p+j)!='\0') {      //条件可用p[j]!='\0';

//            len++;

//        //}

//    }

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


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

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

//    int *p=arr;

//    int a=10;

//    p=&a;//指针的重指向


//char str1[20]="dshfjkhgkdf";

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

//    printf("\n");

////空指针

//    int *p=NULL;

//    //int a=10;

//    int *p1=&a;

//    p=&a;//

//    p++;//高位移动int 4个字节

//   // *p//取值

//    //p[1]//语法糖

    return 0;

}

(2)MyFunction.h

//

//  MyFunction.h

//  c8_指针

//

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

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

//


#import <Foundation/Foundation.h>

////模拟投票过程,4人进行投票,投票后,根据票数对4人从小到大进行排序

//姓名票数

struct person{

    char personName[20];

    int num;

};

typedef struct person person;

//投票

//参与投票的人数

void vote(person per[],int perNum);

void test(int *b);

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

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

int test1();

int test2();

(3)MyFunction.m

//

//  MyFunction.m

//  c8_指针

//

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

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

//


#import "MyFunction.h"

////模拟投票过程,4人进行投票,投票后,根据票数对4人从小到大进行排序

void vote(person per[],int perNum){

    for (int i=0; i<perNum; i++) {

        char c=0;

        scanf("%c\n",&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){

    *a+=20;

    printf(".ma的地址:%p\n",a);

};

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

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

    int temp=0;

    temp=*a;

    *a=*b;

    *b=temp;

    //交换的是内容,不是地址.

    printf("a=%d b=%d",*a,*b);

    printf("\n");

}

////

int test1(){

    int g=10;

    return g;

};

int test2(){

    int g;

    return g;

};

0 0