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

[求助]c++ syntax about 2d array

HJin 发布于 2007-06-21 14:35, 1495 次点击
I wrote a function

// a is 2d --- n x n matrix
void f(int** a, int n); // (1)

int main()
{
const int n=3;
int a[n][n]; // (2)

/**
Question: how do I pass the matrix a to f()?

Don't modify lines (1) and (2).

Thanks.
*/
return 0;
}
13 回复
#2
tobyliying2007-06-21 16:39

你到底是要干什么,
请说具体点
做这个矩阵 是干什么用。
具体有什么作用呀

#3
野比2007-06-21 20:27
I suppose that you wanna pass and use the matrix inside the f() function...
I made it like this:
--------------
int* b=&a[0][0]; //a pointer to the start of the array
int** c=&b; //a pointer points to the pointer which points to the array... wow.....tongue twister... (^^!)
//now you can use the c pointer as the parameter of f() instead...
#4
野比2007-06-21 20:44
回复:(HJin)[求助]c++ syntax about 2d array
here's my test result..

只有本站会员才能查看附件,请 登录

#5
aipb20072007-06-21 21:58
TO HJin:
你想用指想指针的指针指向一个2d数组似乎不能实现。因为使用数组名,将指向数组的第一个元素,这里2维数组即是指向一个数组,然后你想进一步去实现这个子数组的名字转换,似乎是不允许的,至少我没看到过。

如果你想用指针去指向一个2维数组,可以这样:
const int n=3;
int a[n][n]; // (2)
int (*p)[n] = a;

这样一来,你的函数参数就要边一下了,
void f(int (*p)[3], int n); // (1)

正如你所见,数组第2维大小必须固定。


我只说了我知道了,也许有其他更好的方法,期待!
#6
HJin2007-06-22 02:51

1. 野比's soln does not work, test the program below yourself:

[CODE]#include <iostream>
using namespace std;
void f(int** a, int n)
{
int i;
int j;
for(i=0; i<n; ++i)
for(j=0; j<n; ++j)
a[i][j] = i+j;
}

int main(int argc, char** argv)
{
const int n=3;
int a[n][n];
int* b = &(a[0][0]);
int** c = &b;
f(c, n);
return 0;
}[/CODE]

2. tobyliying --- I just want to understand a C++ syntax.

3. I wrote the function f(), assuming that I will pass a dynamically allocated 2d array to it. Then I changed idea --- I just want to pass a 2d array on the program stack (instead of the heap).

Your suggestion is good --- but I don't know n beforehand. Thus "pointer to array of lenght n" approach is not appropriate.

#7
野比2007-06-22 21:03
what about this??
make a pointer array(1-d)..

int* b[n];

then.. store each address of the 2-d array rows to b[n]..

for(int i=0;i<n;++i){
b[i]=&a[i][0]; //suppose the 2-d array is a[n][n]
}

now you can use b as parameter of f() directly...
(b is a pointer to a pointer array, so it is in the form int** , just like argv in "char** argv")
#8
aipb20072007-06-22 21:32
野比's way is OK!
#9
野比2007-06-22 21:36
看到那个char** argv我突然明白HJin想做什么了...
Am I right, HJin?
#10
HJin2007-06-22 23:38
野比's way

[CODE]int* b=&a[0][0]; //a pointer to the start of the array
int** c=&b; //a pointer points to the pointer which points to the array... wow.....tongue twister... (^^!)
//now you can use the c pointer as the parameter of f() instead...[/CODE]

on my system gives access violation error. b points to the start of the 2d array, c is just the address of the b variable, so that

[CODE]c[2][2] = 1;[/CODE]
fails.




#11
aipb20072007-06-22 23:46
以下是引用HJin在2007-6-22 23:38:58的发言:
野比's way

[CODE]int* b=&a[0][0]; //a pointer to the start of the array
int** c=&b; //a pointer points to the pointer which points to the array... wow.....tongue twister... (^^!)
//now you can use the c pointer as the parameter of f() instead...[/CODE]

on my system gives access violation error. b points to the start of the 2d array, c is just the address of the b variable, so that

[CODE]c[2][2] = 1;[/CODE]
fails.




I think you misunderstand.
int *p[n];
p[n] = &a[0][0]
……

then
int **ptr = p;

try this.

#12
野比2007-06-24 23:10
以下是引用HJin在2007-6-22 23:38:58的发言:
野比's way

[CODE]int* b=&a[0][0]; //a pointer to the start of the array
int** c=&b; //a pointer points to the pointer which points to the array... wow.....tongue twister... (^^!)
//now you can use the c pointer as the parameter of f() instead...[/CODE]

on my system gives access violation error. b points to the start of the 2d array, c is just the address of the b variable, so that

[CODE]c[2][2] = 1;[/CODE]
fails.

Hey budy, I posted a new method on the 7th floor.. SEE ?

Take a look..

#13
HJin2007-06-25 01:54
thanks 野比 and aipb2007, got your guys' idea.
#14
野比2007-06-25 19:51
Not at all...
1