zoukankan      html  css  js  c++  java
  • c语言的指针基础


    这里对a和b进行了了交换,如果不是传了指针进去int* a和b 那a和b的值就不会交换

    #include <stdio.h>
    //作为函数的参数
    void jiaohuan(int *a,int *b){
    int temp=0;
    temp=*a;
    *a=*b;
    *b=temp;
    };
    int main(){

      //作为函数参数
      int m = 5;
      int n = 6;
      jiaohuan(&m,&n);
      printf("%d ",m);
      printf("%d ",n);

    }

        

    函数指针
    int add(int a,int b){
    return a+b;
    }
    int main(){
    int (*sum1)(int a,int b)= &add;
    
    int result=(*sum1)(5,6);
    
    printf("%d
    ",result);
    //用法二
    // int (*sum)(int a,int b) =add; 


    // int result = (*sum)(5,6);
    // printf("%d ",result);


    // int (*sum)(int a,int b) =add; 


     // int result = (*sum)(5,6);
    // printf("%d ",result);


    }

     指向常量的常量指针 ,

    int a=7;
    const int *const p=&a;
    printf("%d
    ",*p);

      定义一个指针,指针的大小

    char str2[] ="abc"
    char *str="abc";
    int *p =(int *)str; //强制转为int类型
    int a = 5;
    int *p = &a;
    //输出a的值
    printf("%d",a);
    //输出a的地址
    printf("%p",&a);
    *P = 8; //a=8

    //输出这个数据类型的大小
    printf("%lu ",sizeof(int));

    
    

    //输出指针的大小

    
    

    printf("%lu ",sizeof(p)); 

     

     空指针

    #include <stdio.h>
    int main(){ int a=5; //没有定义类型的指针 void *p =&a; printf("%d ",*((int *)p)); }
    值针数组
    
    
    #include <stdio.h>
    int main(){

    //定义一个值针数组

    int a=5;
    int b=6;
     int *arry[2];
     arry[0]=&a;
     arry[1]=&b;
     printf("%d
    ",arry[0]);
     printf("%d
    ",arry[1]);
    }
    指针的指针 // 用的比较多
    #include <stdio.h>
    int main(){
    //指针的指针 
    int a=5;
    int *p=&a;
    int **P1=&p;
    printf("%d ",**P1);
    }
    #include <stdio.h>

    int main(){

    //指向常量的指针
     int a=6;
     //const int *p =&a;
     int const *p=&a;
     printf("%d
    ",*p);
    
    }
     

     }

    基础的,比较简单!但要灵活运用就比较难了!

    要记住指针只是一个指向地址的工具,因为这个我们就可一从根本去取一个值,和改变一个值。(仅仅代表个人立场)

  • 相关阅读:
    希腊字母写法
    The ASP.NET MVC request processing line
    lambda aggregation
    UVA 10763 Foreign Exchange
    UVA 10624 Super Number
    UVA 10041 Vito's Family
    UVA 10340 All in All
    UVA 10026 Shoemaker's Problem
    HDU 3683 Gomoku
    UVA 11210 Chinese Mahjong
  • 原文地址:https://www.cnblogs.com/kangniuniu/p/4921702.html
Copyright © 2011-2022 走看看