zoukankan      html  css  js  c++  java
  • c指针点滴三(指针运算)

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 void main3()
     5 {
     6     int num = 89;
     7     int *p = &num;
     8     p++;//不可预测的值 指针++就是按照指针的类型大小前进一个类型的大小
     9     //如果是int就前进4个
    10     printf("%d",*p);//指针++只有在数组才有意义
    11 }
    12 
    13 //void main4()
    14 //{
    15 //    int a[5];
    16 //    int i = 0;
    17 //    printf("%p",a);
    18 //
    19 //    for(int *p=a;p<a+5;p++)//切记 指针++  是移动sizeof指针类型的字节
    20 //    {
    21 //        *p = i;//对指针指向的值赋值
    22 //        i++;
    23 //    }
    24 //    system("pause");
    25 //}
    26 
    27 void main5()
    28 {
    29     int a[5] = {1,2,3,4,5};
    30     int *p5 = &a[2];
    31     printf("%d",a[2]);
    32     p5 = p5+ 2;//相当于在数组内部向后移动两个元素的大小
    33 
    34     //double *p = 0x300400
    35     //p = p+2 0x300416
    36     printf("
    %d",*p5);//5 &a[4]
    37     getchar();
    38 }
    39 
    40 void main6()
    41 {
    42     double num = 10;
    43     double *p = &num;
    44     p  = p-465436;//指针的加减法只能在数组有意义 而且容易越界
    45                 //一个exe不能读写其他exe
    46     printf("%f",*p);
    47 }
    48     
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 //判断两个指针是否相等 就看是否指向同一个地址
     5 void main()
     6 {
     7     int num = 32;
     8     int *p = &num;
     9     int *p1 = &num;
    10     if(p1==p2)
    11     {
    12         printf("指向同一个女人是情敌");
    13     }else
    14     {
    15         printf("不是情敌了");
    16     }
    17 }
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 //指针的赋值运算 一般就是传递地址
     5 //根据地址改变内容
     6 void main4()
     7 {
     8     int num = 10;
     9     int *p = &num;
    10     printf("
    %p,%p",p,&num);//一样
    11 
    12     *p = 4;
    13     printf("
    %d",num);
    14 
    15     int *px = p;
    16     *px = 3;
    17     printf("
    %d",num);//同样等价操作num
    18 
    19     system("pause");
    20 }
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 void main4()
     4 {
     5     int a[10]={1,2,3,4,5,6,7,8,9,0};
     6     int i;
     7     printf("
    %d",*(a+4));//5 &a[4]
     8 
     9     getchar();
    10 
    11 }
    12 void main3()
    13 {
    14     int a[10]={1,2,3,4,5,6,7,8,9,0};
    15     int i;
    16     printf("%x",a);//被编译器为首地址
    17 
    18     //此处断电查看数组元素
    19     for(i=0;i<10;i++)
    20     {
    21         printf("
    %d,%d",a[i],*(a+i));
    22         printf("%x,%x",&a[i],a+i);//等价关系
    23     }
    24 
    25     //指针遍历
    26     for(int *p=a;p<a+10;p++)
    27     {
    28         *p = *p - 1;
    29         printf("
    %d,%x",*p,p);
    30     }
    31     getchar();
    32 }
    33 void main2()
    34 {
    35     int num = 10;
    36     int *p1 = &num;//地址的赋值
    37     int *p2 = p1;//指针的赋值
    38 
    39     *p2 = 3;
    40     printf("%d,%d,%d",num,*p1,*p2);//全是3
    41 }
    42 void main1()
    43 {
    44     int num = 10;
    45     //int *p = a;
    46     //*p = a;//整数和指针最好不要直接运算 c只是警告 但是c++一定报错
    47 
    48 }
  • 相关阅读:
    Codeforces 50A
    Codeforces 50A
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
    Python Ethical Hacking
  • 原文地址:https://www.cnblogs.com/lanjianhappy/p/5960056.html
Copyright © 2011-2022 走看看