C语言回顾 八 指针

指针
#import <Foundation/Foundation.h>
//指针作为参数可以实现两个数的交换(指针x和y操作是传过来的实参a,b所对应的地址空间)
void changeValue1(int *x, int *y);
void changeValue1(int *x, int *y)
{
    printf("交换前x=%d, y=%d\n",*x, *y);
    int temp = *x;
    *x = *y;
    *y = temp;
    printf("交换后x=%d, y=%d\n",*x, *y);
}
//打印数组全部元素
void printfArray(int *arr,int count);  //(int arr[], int count)也可以
void printfArray(int *arr,int count)   //数组作为参数,退化成指针,所以打印Arr所占字节的时候,int *arr 和 int arr[]的形式都是执着呢所占的字节数(8);
{
//    printf("*****lu\n",sizeof(arr));
    for (int i = 0; i < count; i++) {
        printf("%d ",*(arr + i));
    }
}
int main(int argc, const char * argv[])
{
//指针定义 类型 *变量名 = 初值  (int *p = NULL)
    //*的作用1  指针定义时,只为了说明该变量是一个指针变量
    //类型的作用1:‘类型*’组成该变量的类型
    //类型的作用2  决定取值时需要访问的字节个数
    //例  int *p = NULL;
    //类型是int * (整型指针),初始值NULL 恒等于 0;
    /*
    int *p = NULL;
    printf("%p \n",p);//打印地址占位符 %p
    // *的作用2 : 取值运算符 *
    printf("%d", *p);
    //&取址运算符,取出某个变量的地址,例如&a
    int a = 3;
    int b = 5;
    int *p = &a;  //把a的地址赋值给p
    printf("&a=%p\n",&a); //打印a的地址
    printf("p= %p\n",p);  //打拼p的内容
    printf("%d\n",a);//直接访问
    printf("%d\n",*p);//通过指针访问p保存的地址里面的内容
    //给指针变量p重新赋值 (指针重指向)
    p = &b;
    printf("p= %p\n",p);
    printf("&b=%p\n",&b);
    printf("%d\n",b);
    printf("%d\n",*p);
    //改变p存放的地址的内容(给该块空间重新赋值)
    *p = 7;
    printf("%d\n",*p);
    //指针的算术运算
    int a = 4;
    int *p = &a;
    printf(" %p\n",p);//原来的地址
    p++;  //p++向高位移动四个字节 p--向低位移动四个字节
    printf("p++%p\n",p);
    //注意; 指针类型决定移动几个字节
    //  *p , *(p + 1) , *p + 1
    int a = 3, b = 5;
    int *p = &a;
    printf("%d\n",*p);
    p = &b;
    *p = 100;
    printf("%d\n",b);
    
    short a = 3;
    short *p = &a;
    printf("%p\n",p);
    printf("%p\n",&a);
    printf("%hd\n",*p);//通过p
    printf("%hd\n",a);//直接
     
    //可以直接给指针一个地址,但不建议写
    int a = 4;
    printf("%p\n",&a);
    int *p = 0x7fff5fbff7ee;
    printf("%p\n",p);
    printf("%d\n",*p);

    //操作哪一类型的变量就定义同类型的指针变量
    short a = 7;  //指针类型要与变量的类型保持一致
    short *p = &a;
    short *q = &a;
    printf("%d\n",*p);
    printf("%d\n",*q);
    
    //指针与数组
    //数组名就是整个数组的首地址(不需要再加&),array和&array[0]是等价的
    int a[5] = {1, 5, 7, 8, 9};
    int *p = a;//把数组a的首地址赋值给p
    printf("%d\n",*p);        //1
    printf("%d\n",*(p+1));   //5
    printf("%d\n",*(p)+1);   //2
    *(p + 2) = 6;
    printf("%d\n",*(p + 2)); //6
    //通过指针来操作数组的时候,指针变量p和数组名是等价的,有四种方式 a[i]  *(a+i)  p[i]  *(p+i)
    //但是数组名与指针变量是不同的,不同点1:数组名是常量地址,而指针变量是指针(存储地址的变量),不同点2: 所占的字节不同,数组所占字节与类型有关,而指针只与操作系统有关;
    printf("%d\n",a[0]);  //1
    printf("%d\n",*p);    //1
    printf("%d\n",*a);    //1
    printf("%d\n",p[0]);  //1
    printf("%lu\n",sizeof(a));  //20
    printf("%lu\n",sizeof(p));  //8
    char *q = NULL;
    char *q1 = NULL;
    printf("%lu\n",sizeof(q));  //8
    printf("%lu\n",sizeof(q1)); //8
    //32位系统指针占4个字节,64位系统占8个字节;
     
    printf("%lu\n",sizeof(a) / sizeof(a[0])); //20 / 4 = 5;
    printf("%lu\n",sizeof(p) / sizeof(p[0])); // 8 / 4 = 2; //%lu是无符号长整型
    //
    short a[4] = {3, 7, 9, 1};
    int *p = a;
    char *p2 = a;
    printf("%d\n",*p);
    printf("%d\n",*p2);
    //字符串循环遍历打印
    char string[10] = "iPhone";
    char *p = string;   //地址
    for (int i = 0; i < strlen(string); i++) {
        printf("%c",p[i]); //四种方式 a[i]  *(a+i)  p[i]  *(p+i), a为数组名

    }
    //字符串整体操作
    printf("\n%s\n",string); //打印字符串
    printf("%s\n",p);      //通过指针打印字符串
    printf("%s\n",p + 3);  //从下标为3的位置开始到最后
    //单个元素进行赋值
    *(p + 1) = 's';
    
//字符串整体进行赋值
    strcpy(string , "sssss");
    printf("%s\n",p);
    
//常量区(常量区可以访问,但是不能修改)     //访问每个字符
    char *string = "iPhone";

    for (int i = 0; i < strlen(string); i++) {
        printf("%c ",*(string + i));
    }
     //打印整个字符串
    printf("\n%s\n",string);
    
//赋值
   //    *(string + 1) = 's'; //错误点 : 常量区可以访问,但是不能修改
   //    printf("%s\n",string);
     
   //通过指针可以计算出字符串的元素个数吗?    char string[] = "iPhone";

    char *p = string ;
    int i = 0;
    while (*p != '\0') {
        i++;
        p++;
    }
    printf("len= %d",i);
    
//指针作为参数可以实现两个数的交换(指针x和y操作是传过来的实参a,b所对应的地址空间)
    int a = 3, b = 5;
    printf("交换前a=%d, b=%d\n",a, b);
    changeValue1(&a, &b);
    printf("交换后a=%d, b=%d\n",a, b);
    */
    
    //打印所有的数组元素
    int array[5] = {2, 5, 7, 9, 21};
    //打印所有的数组元素
    printfArray(array, 5);
    
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值