zoukankan      html  css  js  c++  java
  • 指针函数、指针数组、函数指针、数组指针、函数指针数组。

    函数的入口地址(首地址):函数名

    一、指针函数:指针函数实质是一个函数,其返回值是一个指针,是一个地址。

      定义:type *function(type A,type B);

      例:int *fun(int a,float c);

    #include "stdio.h"

    static int *fun(int a,int b); // 指针函数

    int *fun(int a,int b)
    {
    return a<b?&a:&b;
    }


    int GetValue(int value)
    {
    return value;
    }
    int main()
    {
    int a=500;
    int b=1000;
    int res1=*fun(a,b);
    int res2=*fun(b,a);

    printf("%d,%d,%d ",res1,res2,GetValue(*fun(a,b)));
    return 0;
    }

     

    二、指针数组:指针数组实质是一个数组,数组里面储存的是指针。

     定义:type *array[Num];

      例:char *array[10];

    #include "stdio.h"
    
    char array[][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    char *arr1[3];
    int main()
    {
        char i=0;
        char j=0;
        for(;i<3;i++){
            arr1[i]=array[i];
        }
        i=0;
        for(;i<3;i++){
            for(j=0;j<4;j++){
                printf("%d ",arr1[i][j]);
            }
            printf("
    ");
        }
        return 0;
    }
    #include<stdio.h>
    int main(){
        int arr[]={1,2,3,4,5};
        int *ptr[5];  // 指针数组,储存的是地址 
        int i=0; 
        for(;i<5;i++){
            ptr[i]=&arr[i];
            printf("*ptr[%d]=%d
    ",i,*ptr[i]);
        }
        return 0;
    }

    三、函数指针:函数指针其实质就是一个指针,函数指针指向一个函数的首地址。

      定义:1、type (* function)(type, type);

         2、type (* function)(type A,type B);

         3、type(*function)();

      例:int (* fun)(char *,float) ;

           int (* fun)(char *name,float sum);

                   int (*fun)();

    #include "stdio.h"
    
    typedef int (*Calc)(unsigned char,unsigned char);  // 定义一个函数指针 
    
    static int getValue(unsigned char a,unsigned char b);
    
    int getValue(unsigned char a,unsigned char b)
    {
        return a+b;
    }
    
    int (*t)(unsigned char,unsigned char);  // 函数指针声明  
    int (*tt)(unsigned char a,unsigned char b);  // 函数指针声明 
    
    int main()
    {
        unsigned char a=200;
        unsigned char b=250;
        t=getValue;
        tt=getValue;
        Calc sum=getValue;  // 和 typedef struct相似。 
        printf("the sum is:%d
    ",sum(a,b));
        printf("the sum is:%d
    ",t(a,b)); 
        printf("the sum is:%d
    ",tt(a,b));  
        return 0;
    }
    #include<stdio.h> 
    int (*g_funp)()=NULL;
    
    int fun(int a,int b)
    {
        return a+b;
    }
    
    int main()
    {
        int c;
        g_funp=fun;
        c=g_funp(300,400);
        printf("%d
    ",c);
        return 0;
    }

    四、数组指针:数组指针实质是指针,指向一个数组。

      定义:type (*array)[Num];

      例:char (*array)[10];

    #include "stdio.h"
    
    char array[][4]={1,2,3,4,5,6,7,8,9,10,11,12};
    int main() { char i=0; char j=0; char (*ptr)[4]; ptr=array; for(;i<3;i++){ for(j=0;j<4;j++) printf("%d ",ptr[i][j]); printf(" "); } return 0; }

    五、函数指针数组:其元素都是函数指针的数组,储存的是函数的入口地址。

      定义:type(*array [num])();

      例:int (*array[])();

        int (*array[2])();

        int (*array[])(int,int);

        int (*array[2])(int,int);

    #include<stdio.h> 
    int (*g_funpOn[])();  // [Warning] array 'g_funpOn' assumed to have one element
    int (*g_funpTw[2])();
    int (*g_funpTh[])(int,int);  // [Warning] array 'g_funpTh' assumed to have one element
    int (*g_funpFo[2])(int,int);
    int sum(int a,int b){
        return a+b;
    }
    
    int sub(int a,int b){
        return a-b;
    }
    
    int main()
    {
        g_funpOn[0]=sum;
        g_funpOn[1]=sub;
        
        g_funpTw[0]=sum;
        g_funpTw[1]=sub;
        
        g_funpTh[0]=sum;
        g_funpTh[1]=sub;
        
        g_funpFo[0]=sum;
        g_funpFo[1]=sub;
        
        printf("g_funpOn[0]=%d
    ",g_funpOn[0](300,400));
        printf("g_funpOn[1]=%d
    
    ",g_funpOn[1](300,400));
        
        printf("g_funpTw[0]=%d
    ",g_funpTw[0](300,400));
        printf("g_funpTw[1]=%d
    
    ",g_funpTw[1](300,400));
        
        printf("g_funpTh[0]=%d
    ",g_funpTh[0](300,400));
        printf("g_funpTh[1]=%d
    
    ",g_funpTh[1](300,400));
        
        printf("g_funpFo[0]=%d
    ",g_funpFo[0](300,400));
        printf("g_funpFo[1]=%d
    
    ",g_funpFo[1](300,400));
        return 0;
    }

     六:一级指针访问二维数组

    #include<stdio.h>
    int main(){
        int arr[][3]={1,2,3,4,5,6,7,8,9,10,11,12};
        int *ptr=arr[0];  // 指针数组,储存的是地址 
        int i=0; 
        for(;i<4*3;i++)
            printf("ptr[%d]=%d
    ",i,*(ptr+i));
        return 0;
    }

     

  • 相关阅读:
    创建类type (底层代码)
    getitem, setitem, delitem (把类实例化成字典的类型)
    类的三种方法(静态方法,类方法,属性方法)
    class(类的使用说明)
    Java泛型
    Java时间操作常用api
    JVM加载class文件的原理机制
    int和Integer区别
    final
    Anonymous Inner Class (匿名内部类) 是否可以extends(继承)其它类,是否可以implements(实现)interface(接口)?
  • 原文地址:https://www.cnblogs.com/ligei/p/12431032.html
Copyright © 2011-2022 走看看