| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1492 人关注过本帖
标题:[求助]c++ syntax about 2d array
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
 问题点数:0 回复次数:13 
[求助]c++ syntax about 2d array
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;
}
搜索更多相关主题的帖子: syntax array 
2007-06-21 14:35
tobyliying
Rank: 1
等 级:新手上路
帖 子:49
专家分:0
注 册:2007-4-16
收藏
得分:0 

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

2007-06-21 16:39
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
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...

女侠,约吗?
2007-06-21 20:27
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
回复:(HJin)[求助]c++ syntax about 2d array
here's my test result..

图片附件: 游客没有浏览图片的权限,请 登录注册


女侠,约吗?
2007-06-21 20:44
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
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维大小必须固定。


我只说了我知道了,也许有其他更好的方法,期待!

Fight  to win  or  die...
2007-06-21 21:58
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 

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.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-22 02:51
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
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")

女侠,约吗?
2007-06-22 21:03
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 
野比's way is OK!

Fight  to win  or  die...
2007-06-22 21:32
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 
看到那个char** argv我突然明白HJin想做什么了...
Am I right, HJin?

女侠,约吗?
2007-06-22 21:36
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
野比'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 am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-06-22 23:38
快速回复:[求助]c++ syntax about 2d array
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.030200 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved