今天公司实习的学弟,问我指针问题,我就给他总结了几个,这里也分享给大家。
当然,不是那么全面,不过学会这些,应付简单的指针问题还是OK的。
这里我把注释都删掉了,让大家自己研究去吧,当然,如果有哪里错了,也请大家指出。谢谢。
#include <stdio.h>
int *(*Fun(int))[5];
typedef *(*(*PFUN)(int))[5];
int main()
{
int p1[5];
int (*p2)[5];
int i = 0;
for (i=0;i<5;i++)
{
p1[i] = i+1;
}
p2 = &p1;
printf("-> 01 <-\n");
for (i=0;i<5;i++)
{
printf("%d\n",(*p2)[i]) ;
}
//
printf("-> 02 <-\n");
int (*(*p3))[5] = NULL;
p3 = &p2;
for (i=0;i<5;i++)
{
printf("%d\n",(**p3)[i]) ;
}
//
printf("-> 03 <-\n");
int *p5[5];
for (i=0;i<5;i++)
{
p5[i] = new int;
*p5[i] = i+100;
}
int *(*p4)[5] = NULL;
p4 = &p5;
for (i=0;i<5;i++)
{
printf("%d\n",*(*p4)[i]) ;
delete (*p4)[i];
}
//
printf("-> 04 <-\n");
int **p6 = (int **)Fun(200);
for (i=0;i<5;i++)
{
printf("%d\n",*p6[i]);
delete p6[i];
}
delete p6;
///
printf("-> 05 <-\n");
PFUN pFun = Fun;
int **p7 = (int **)pFun(300);
for (i=0;i<5;i++)
{
printf("%d\n",*p7[i]);
delete p7[i];
}
delete p7;
///
printf("-> 06 <-\n");
int *(*(*p8[5])(int))[5];
for (i=0;i<5;i++)
{
p8[i] = (int *(*(__cdecl *)(int))[5])pFun( (i+1)*500);
//*(p8+i) = (int *(*(__cdecl *)(int))[5])pFun( (i+1)*500);
}
for (i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
printf("%d\n",*((int**)(p8[i]))[j]);
delete ((int**)(p8[i]))[j];
}
delete (int*)(p8[i]);
}
return 0;
}
int *(*Fun(int nValue))[5]
{
/*
static int *p[5];
for (int i=0;i<5;i++)
{
p[i] = new int;
*p[i] = i + nValue;
}
return &p;
*/
int **p = new int *[5];
for(int i=0;i<5;i++)
{
p[i] = new int;
*p[i] = i + nValue;
}
return (int *(*)[5])p;
}
转载于:https://blog.51cto.com/suqianbozai/1168793