函数指针,该指针指向的函数的返回值是数组,该数组里放的是函数指针,该函数的返回值是数组。。。无限循环。。。

#include <stdio.h>
#include <stdlib.h>

void fun(int a){
  printf("fun:%d\n", a);
}

void fun1(void (*f)(int),int a){
  f(a);
}

int gun(int a){
  printf("gun:%d\n", a);
}

int gun1(int a, int(*f)(int)){
  printf("gun1:%d\n", a);
}

//函数的返回值是指针,该指针指向数组,数组可以一维数组也可以是二维数组,本函数是二维数组,数组里放的是int
int (*hun(int a))[]{
  int (*p)[3] = (int(*)[3])malloc(sizeof(int[3]) * 2);
  (*p)[0] = a+1;
  //p[0][0] = a+1;
  p[0][1] = a+2;
  p[0][2] = a+3;
  (*(p+1))[0] = a+11;
  //下面写法是错误的,p+1两侧必须加括号
  //(*p+1)[0] = a+11;
  p[1][1] = a+22;
  (*(p+1))[2] = a+33;

  return p;
}

//函数的返回值是指针,该指针指向数组,数组可以一维数组也可以是二维数组,本函数是一维数组,数组里放的是int
int (*hun2(int a))[]{
  int *p = (int*)malloc(sizeof(int) * a);
  for(int i = 0; i < a; ++i){
    p[i] = a+i;
  }

  return p;
}

//函数的返回值是指针,该指针指向数组,数组可以一维数组也可以是二维数组,本函数是二维数组,数组里放的是int指针
int *(*hun1(int a))[]{
  int* (*p)[3] = (int*(*)[3])malloc(sizeof(int*[3]) * 2);
  int *w1 = (int*)malloc(sizeof(int));
  *w1 = 10+a;
  int *w2 = (int*)malloc(sizeof(int));
  *w2 = 20+a;
  int *w3 = (int*)malloc(sizeof(int));
  *w3 = 30+a;
  int *w4 = (int*)malloc(sizeof(int));
  *w4 = 40+a;
  int *w5 = (int*)malloc(sizeof(int));
  *w5 = 50+a;
  int *w6 = (int*)malloc(sizeof(int));
  *w6 = 60+a;
  (*p)[0] = w1;
  p[0][1] = w2;
  p[0][2] = w3;
  (*(p+1))[0] = w4;
  p[1][1] = w5;
  (*(p+1))[2] = w6;
  
  return p;
}
int jun(int a, int b){
  printf("a:%d,b:%d\n",a,b);
  return a+b;
}
//函数的返回值是指针,该指针指向数组,数组可以一维数组也可以是二维数组,本函数是一维二级指针数组,数组里放的是int指针
int*(*nun(int* a))[]{
  int** p = (int**)malloc(sizeof(int*) * *a);
  for(int i = 0; i < *a; ++i){
    int *pi = (int*)malloc(sizeof(int));
    *pi = i+20;
    p[i] = pi;
  }
  return p;
}

//返回一维的二级指针数组
int** mun1(int* a){
  int** p = (int**)malloc(sizeof(int*) * *a);
  for(int i = 0; i < *a; ++i){
    int *pi = (int*)malloc(sizeof(int));
    *pi = i+11;
    p[i] = pi;
  }
  return p;
}

int main(){
  //指针数组,数组里是函数指针,指向的函数没有返回值,1个int参数
  void (*a[1])(int);
  a[0] = &fun;
  (*a[0])(1);
  //指针数组,数组里是函数指针,指向的函数没有返回值,2个参数。第一个参数是函数指针,指向的函数没有返回值,1个int参数;第二个参数是int
  void (*b[1])(void(*)(int),int);
  b[0] = &fun1;
  (*b[0])(a[0],10);
  
  //指针数组,数组里是函数指针,指向的函数的返回值是int,1个int参数
  int (*c[1])(int);
  c[0] = &gun;
  (*c[0])(3);
  //指针数组,数组里是函数指针,指向的函数的返回值是int,2个参数。第一个参数是int;第二个参数是函数指针,指向的函数的返回值是int,1个int参数。
  int(*d[1])(int, int(*)(int));
  d[0] = &gun1;
  (*d[0])(120,c[0]);

  //指针数组,数组里是函数指针,指向的函数的返回值是数组指针,数组里是int,1个int参数。  
  int (*(*e[1])(int))[];
  e[0] = &hun;
  int (*p)[3] = (*e[0])(3);
  for(int i = 0; i < 2; ++i){
      for(int j = 0; j < 3; ++j){
      printf("%d ", p[i][j]);
    }
    printf("\n");
  }
  free(p);
  e[0] = &hun2;
  int aa = 3;
  int *pp = (*e[0])(aa);
  for(int i = 0; i < aa; ++i){
    printf("hun2 %d ", pp[i]);
  }
  printf("\n");

  //指针数组,数组里是函数指针,指向的函数的返回值是数组指针,数组里是int指针,1个int参数。 
  int *(*(*g[1])(int))[];
  g[0] = &hun1;
  int* (*p1)[3] = (*g[0])(10);
  for(int i = 0; i < 2; ++i){
    for(int j = 0; j < 3; ++j){
      printf("%d ", *p1[i][j]);
    }
    printf("\n");
  }
  free(p1);

  int (*j[1])(int,int);
  j[0] = &jun;
  (*j[0])(2,3);

  //指针数组,数组里的指针指向的是函数,该函数的返回值是int,有2个int参数
  int (*(*k)[2])(int,int) = (int**)malloc(sizeof(int*) * 2);
  //(*k)[2] = (int(*)[2])malloc(sizeof(int*[2]) * 2);
  //  int (*p)[3] = (int(*)[3])malloc(sizeof(int[3]) * 2);
  (*k)[0] = &jun;
  (*k)[0](3,4);
  (*k)[1] = &jun;
  (*k)[1](30,42);
  free(k);

  //函数指针,该指针指向的函数的返回值是数组指针,该指针指向数组,数组里放的是int指针
  int*(*(*m)(int*))[];
  m = &mun1;
  int mi = 3;
  int** mp = *m(&mi);
  for(int i = 0; i < mi; ++i){
    printf("mp[%d] = %d ", i, *mp[i]);
    printf("mp+%d = %d ", i, **(mp+i));    
  }
  printf("\n");

  int ni = 2;
  int** np = nun(&ni);
  for(int i = 0; i< ni; ++i){
    printf("np[%d] = %d ", i, *np[i]);
    printf("np+%d = %d ", i, **(np+i));    
  }
  printf("\n");
}

执行结果:

fun:1
fun:10
gun:3
gun1:120
4 5 6 
14 25 36 
hun2 3 hun2 4 hun2 5 
20 30 40 
50 60 70 
a:2,b:3
a:3,b:4
a:30,b:42
mp[0] = 11 mp+0 = 11 mp[1] = 12 mp+1 = 12 mp[2] = 13 mp+2 = 13 
np[0] = 20 np+0 = 20 np[1] = 21 np+1 = 21 
posted @ 2018-06-12 14:28  小石王  阅读(235)  评论(0编辑  收藏  举报