注册 登录
编程论坛 C++教室

问一个语法问题

leeco 发布于 2007-10-13 22:46, 791 次点击
想让函数返回一个数组指针,函数类型应该如何写?
11 回复
#2
aipb20072007-10-13 23:08
以指针的形式返回数组?

#3
coachard2007-10-13 23:20
#include <stdio.h>
#include <conio.h>
typedef int (*p)[4];
p fun(int (*p)[4]);
p fun(int (*p)[4])
{
return p;
}
int main(void)
{
int a[4][4];
int (*p)[4];
int i,j;
int num=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=++num;
p=fun(a);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
printf("%4d",*(*(p+i)+j));
getch();
return 0;
}

只能想到用typedef来解决了。。。
不知楼下还有哪位有更好的办法~~~

[此贴子已经被作者于2007-10-13 23:21:23编辑过]

#4
leeco2007-10-13 23:56
嗯,不错,这样可以的。谢谢了
#5
HJin2007-10-14 01:46
good, brother
#6
缘吇弹2007-10-14 01:49
请问楼上的朋友是哪里人?
#7
HJin2007-10-14 02:07

#include <iostream>
using namespace std;

/**
Explanation:

The right-left rule:

0. locate the specififer: f in this exmaple
1. look at its right: () in this case, hence f is a function
2. look at its left: * in this case, hence f returns a pointer
3. look at (*f())'s right: [10], hence f is a function which returns a pointer to an array of 10 elements
4. look at (*f())'s left: int, hence f is a function which returns a pointer to an array of 10 ints.
*/
int (*f())[10];


int main()
{
f();

return 0;
}

int (*f())[10]
{
int (*p)[10] = new int[5][10];

return p;
}

#8
缘吇弹2007-10-14 02:15
good way.
#9
aipb20072007-10-14 10:34
这样也可以,学习了,没见过。

int (*f())[10];

f()括号中还可以加参数吧?
#10
缘吇弹2007-10-14 11:03
以下是引用aipb2007在2007-10-14 10:34:30的发言:
这样也可以,学习了,没见过。

int (*f())[10];

f()括号中还可以加参数吧?

测试一下不就知道了.

#11
aipb20072007-10-14 15:23

厉害,从来没见过的语法,可以加参数。
写本本上了

#12
leeco2007-10-18 17:39
以下是引用HJin在2007-10-14 2:07:49的发言:

#include <iostream>
using namespace std;

/**
Explanation:

The right-left rule:

0. locate the specififer: f in this exmaple
1. look at its right: () in this case, hence f is a function
2. look at its left: * in this case, hence f returns a pointer
3. look at (*f())'s right: [10], hence f is a function which returns a pointer to an array of 10 elements
4. look at (*f())'s left: int, hence f is a function which returns a pointer to an array of 10 ints.
*/
int (*f())[10];


int main()
{
f();

return 0;
}

int (*f())[10]
{
int (*p)[10] = new int[5][10];

return p;
}

强的。很好

1