通过2-D,静态定义的阵列? [英] Pass A 2-D, Statically Defined Array?

查看:85
本文介绍了通过2-D,静态定义的阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以传递2-D,静态定义的数组?


这是一个不能工作的一维例子:


void foo(){

int myArray [MAX_SIZE];

bar(myArray);

}


void bar(int * arr){

arr [5] = arr [7];

}


它不起作用,因为myArray是静态定义的。为了使它工作,

你可以改变foo'的bar()调用:


bar(& myArray [0]);


然后它工作正常!


任何方法将相同的技术应用于二维阵列?


void foo(){

int myArray [SIZE_A] [SIZE_B];

bar(?? myArray ??);

}


void bar(int ** arr){

arr [5] [3] = arr [2] [4];

}


一种可能的解决方法是将myArray定义为1-D,但将其用作2-D:


void foo(){

int myArray [SIZE_A * SIZE_B];

bar(& myArray [0]);

}


void bar(int * arr){

arr [5 * SIZE_B + 3] = arr [2 * SIZE_B + 4];

}


有点不优雅。


感谢您的任何建议! --Darel
Da******@gmail.com
http://alienryderflex.com

Is it possible to pass a 2-D, statically defined array?

Here''s a 1-D example that won''t work:

void foo() {
int myArray[MAX_SIZE] ;
bar(myArray);
}

void bar(int *arr) {
arr[5]=arr[7];
}

It won''t work because myArray is statically defined. To make it work,
you can change foo''s bar() call to this:

bar(&myArray[0]);

Then it works fine!

Any way to apply the same technique with a 2-D array?

void foo() {
int myArray[SIZE_A][SIZE_B] ;
bar(??myArray??);
}

void bar(int **arr) {
arr[5][3]=arr[2][4];
}

One possible workaround is to define myArray as 1-D but use it as 2-D:

void foo() {
int myArray[SIZE_A*SIZE_B] ;
bar(&myArray[0]);
}

void bar(int *arr) {
arr[5*SIZE_B+3]=arr[2*SIZE_B+4];
}

Kind-of inelegant, though.

Thanks for any advice! --Darel
Da******@gmail.com
http://alienryderflex.com

推荐答案

Da******@gmail.com 写于01/02/07 16: 30,:
Da******@gmail.com wrote On 01/02/07 16:30,:

是否可以传递2-D,静态定义的数组?


这里是1 -D不行的例子:


void foo(){

int myArray [MAX_SIZE];

bar (myArray);

}


void bar(int * arr){

arr [5] = arr [7] ;

}


它不起作用,因为myArray是静态定义的。
Is it possible to pass a 2-D, statically defined array?

Here''s a 1-D example that won''t work:

void foo() {
int myArray[MAX_SIZE] ;
bar(myArray);
}

void bar(int *arr) {
arr[5]=arr[7];
}

It won''t work because myArray is statically defined.



它以什么方式无法工作? (那你是什么?b $ b意味着静态定义?无论是什么,似乎

与静态关键字无关或与静态

存储持续时间。

In what way does it fail to "work?" (And what do you
mean by "statically defined?" Whatever it is, it seems to
have nothing to do with the "static" keyword or with "static
storage duration.")


为了使其工作,

你可以改变foo 's吧()打电话给:


吧(& myArray [0]);


那么它工作正常!
To make it work,
you can change foo''s bar() call to this:

bar(&myArray[0]);

Then it works fine!



非常奇怪。拿一个没有明显错误的片段

代码,添加一个明显的错误,它工作正常,无论是什么

这意味着什么。

Very strange. Take a fragment of nothing-obviously-wrong
code, add an obvious error, and it "works fine," whatever
that means.


任何方法将相同的技术应用于二维数组?
Any way to apply the same technique with a 2-D array?



技术采取(显然)正确的代码,

添加错误,并声明它工作正常?


DarelRex,我认为你''通过发布一些实际代码而不是零散的释义来消除很多混乱



就目前而言,我不知道你有什么问题

并不能建议解决方案。


-
Er*********@sun.com


< Da ****** @ gmail.comwrote in message

新闻:11 ********************** @ a3g2000cwd.googlegro ups.com ...
<Da******@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...

是否可以传递2-D,静态定义的数组?
Is it possible to pass a 2-D, statically defined array?



这取决于编译时是否知道最右边的N-1数组下标限制。

一个多维数组总是可以作为一维

数组访问。


这个URL可能对你很有用。 />
http://204.122.16.5/~ scs / cclass / int / sx9a.html


一般来说,可以安全地假设编译器可以以某种方式生成
更好的代码如果你这样做:


a [4] [23] [91] [6]


而不是这个:


a [(4 * LIMIT_B * LIMIT_C * LIMIT_D)+(23 * LIMIT_C * LIMIT_D)+(91 *

LIMIT_D] +(6)];


当然这不能保证。


但是,这取决于编译时与运行时间的比较和

您可以向编译器传达多少信息e编译器可以在

中直接访问一个N维数组,只要N-1

最不重要的限制是已知的。


上面的URL有一些例子。

It depends on whether the rightmost N-1 array subscript limits are known at
compile-time.

A multidimensional array can always be accessed as as a one-dimensional
array.

This URL may be interesting for you.

http://204.122.16.5/~scs/cclass/int/sx9a.html

Generally speaking, it is safe to assume that the compiler MIGHT be able to
somehow generate better code if you do this:

a[4][23][91][6]

rather than this:

a[(4 * LIMIT_B * LIMIT_C * LIMIT_D) + (23 * LIMIT_C * LIMIT_D) + (91 *
LIMIT_D] + (6)];

but of course this is not guaranteed.

However, it depends on how much is known at compile-time versus run-time and
how much information you can convey to the compiler. The compiler can in
general access an N-dimensional array directly so long as the N-1
least-significant limits are known.

The URL above has some examples.


2007年1月2日星期二17:02:23 -0500,Eric Sosman< Er ***** ****@sun.com>

写道:
On Tue, 02 Jan 2007 17:02:23 -0500, Eric Sosman <Er*********@sun.com>
wrote:

> Da ****** @ gmail.com写道01/02/07 16:30,:
>Da******@gmail.com wrote On 01/02/07 16:30,:

>是否可以传递2-D,静态定义的数组?

这是一个不起作用的一维例子:

void foo(){
int myArray [MAX_SIZE];
bar(myArray);
}

void bar(int * arr){
arr [5] = arr [7];
}

它不会因为myArray是静态定义的。
>Is it possible to pass a 2-D, statically defined array?

Here''s a 1-D example that won''t work:

void foo() {
int myArray[MAX_SIZE] ;
bar(myArray);
}

void bar(int *arr) {
arr[5]=arr[7];
}

It won''t work because myArray is statically defined.



它以什么方式无法工作? (你用静态定义来表示什么?无论是什么,它似乎都与静态关键字或静态存储无关持续时间。)


In what way does it fail to "work?" (And what do you
mean by "statically defined?" Whatever it is, it seems to
have nothing to do with the "static" keyword or with "static
storage duration.")


>为了使它工作,
你可以改变foo'的bar()调用:

bar(& myArray [0]);

然后它运行正常!
>To make it work,
you can change foo''s bar() call to this:

bar(&myArray[0]);

Then it works fine!



很奇怪。拿一个没有明显错误的代码的片段,添加一个明显的错误,它工作正常,无论那意味着什么。


Very strange. Take a fragment of nothing-obviously-wrong
code, add an obvious error, and it "works fine," whatever
that means.



参数& myArray [0]以什么方式与

参数myArray有什么不同?两者都使用

类型指向int的地址来评估myArray [0]的地址。

删除电子邮件的del

In what way is the argument &myArray[0] any different than the
argument myArray? Both evaluate to the address of myArray[0] with
type pointer to int.
Remove del for email


这篇关于通过2-D,静态定义的阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆