二维数组 [英] two-dimensional array

查看:101
本文介绍了二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在使用二维数组时遇到了一些问题。这是'b $ b故事。

我有两个50x50矩阵(是矩阵的复数吗?)在

编译时定义。

我想在for循环中使用它们,但是根据

迭代,一个是真实的,另一个是虚拟的。在

结束时,迭代我交换了它们。

我正在使用指针,但它不是作为行中的作业工作

15和16来自不同类型。我很确定我之前已经通过

,但不记得我是如何解决它的。有帮助吗?这是我得到的

代码:

-----------------

#include< stdio.h>


#define ROWS 50

#define COLS 50

#define MAXITERS 100

int main(void){

int matrix1 [ROWS] [COLS];

int matrix2 [ROWS] [ COLS];

int ** realMatrix;

int ** dummyMatrix;

int ** swapMatrix;

int i;


realMatrix = matrix1; / *第15行* /

dummyMatrix = matrix2; / *第16行* /


for(i = 0; i< MAXITERS; i ++){

/ *使用Matrix * /

/*...*/

/ *交换矩阵* /

swapMatrix = realMatrix;

realMatrix = dummyMatrix;

dummyMatrix = swapMatrix;

}


返回0;

}

Hi,
I''m having some problems working with two-dimensional arrays. Here''s
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.
I want to work with them inside a for loop, but depending on the
iteration, one is the real one and the other is dummy. At the end of
the iteration I swap them.
I''m working with pointers but it is''nt working as assignments in lines
15 and 16 are from different types. I''m pretty sure I''ve been through
this before but can''t remember how I solved it. Any help? Here''s the
code I''ve got:
-----------------
#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/

for (i=0; i < MAXITERS; i++){
/*Work with Matrix*/
/*...*/
/*Swap Matrix*/
swapMatrix=realMatrix;
realMatrix=dummyMatrix;
dummyMatrix=swapMatrix;
}

return 0;
}

推荐答案

我的* *************@gmail.com 写道:

#include< stdio.h>
#define ROWS 50
#define COLS 50
#define MAXITERS 100
int main(void){
int matrix1 [ROWS] [COLS];
int matrix2 [ROWS] [COLS];
int ** realMatrix;
int ** dummyMatrix;
int ** swapMatrix;
int i;

realMatrix = matrix1; / *第15行* /
dummyMatrix = matrix2; / *第16行* /

#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/




test.c:15:警告:从不兼容的指针类型分配

test.c:16:警告:从不兼容的指针类型分配


正确的声明是:


int(* realMatrix)[COLS];

int(* dummyMatrix)[COLS];

int(* swapMatrix)[COLS];


-Larry Jones

嗯......那可能不是政治上的。 - Calvin



test.c:15: warning: assignment from incompatible pointer type
test.c:16: warning: assignment from incompatible pointer type

The correct declarations are:

int (*realMatrix)[COLS];
int (*dummyMatrix)[COLS];
int (*swapMatrix)[COLS];

-Larry Jones

Hmm... That might not be politic. -- Calvin


指针的声明应该是:

int(* realMatrix)[COLS];

请记住,虽然数组会自动转换为指向表达式中第一个元素的指针,但它与

指针根本不同。

IIRC,这是将旧的B

编程语言转换为C的创新之一。

例如,声明int一个[5];"为五个
整数创建空间,但不存储指向

内存中任何位置的第一个整数的指针。因此,你不能指向一个带有指向

指针(int ** p; p =& a;)指针的数组,因为指向指针的指针必须对物理存在于内存中的指针指向
,但是int a [5];没有

创建一个物理存在于内存中的指针。

因此,你必须使用指向数组的指针(int(* p)[5]; p = & a;"),其中
表示你​​没有指向指针,而是指向一个

数组。你在物理上指向第一个元素,当你取消引用它时,你得到一个数组,它会自动转换为指向第一个元素的

指针。因此,具有讽刺意味(void *)p ==(void

*)* p

注意,这不会使int a [5]无效; int * p; p = a;",因为这个

表达式导出一个指向自动的第一个元素的指针,

并将其分配给p。

我希望这不会让你感到困惑,因为我几乎不理解它,并希望有更熟悉ANSI C的人能够解释它。

祝你好运! br />
Jimmy Hartzell

The declaration for the pointers should be:
int (*realMatrix)[COLS];
Remember, although an array is automatically converted to a pointer to
the first element in expressions, it is fundamentally different from a
pointer.
IIRC, this is one of the innovations made to turn the older "B"
programming language into "C".
For example, the declaration "int a[5];" creates room for five
integers, but does not store a pointer to the first integer anywhere in
memory. Therefore, you can''t point to an array with a pointer to a
pointer ("int **p; p=&a;"), because a pointer to a pointer must point
to a pointer physically present in memory, but "int a[5];" does not
create a pointer physically present in memory.
Thus, you must use a pointer to an array ("int (*p)[5]; p=&a;"), which
recognizes that you are not pointing to a pointer, but rather to an
array. You are physically pointing to the first element, and when you
dereference it, you get an array, which is automatically converted to a
pointer to the first element. Therefore, ironically (void *)p==(void
*)*p
Note that this does not invalidate "int a[5]; int *p; p=a;", as this
expression derives a pointer to the first element of a automatically,
and assigns it to p.
I hope this wasn''t too confusing to you, because I barely understand it
and wish someone more familiar with ANSI C could explain it.
Good luck!
Jimmy Hartzell


我的************** @ gmail.com 写道:
my**************@gmail.com writes:

我使用二维数组时遇到一些问题。这是故事。
我在编译时定义了两个50x50矩阵(是矩阵的复数吗?)。


不,正确的拼写是矩阵。

我想在for循环中使用它们,但取决于
迭代,一个是真实的,另一个是假的。在迭代结束时,我交换它们。
我正在使用指针,但它不能作为行中的赋值工作
15和16来自不同类型。我很确定我以前经历过这个,但不记得我是怎么解决的。有帮助吗?这是我有的代码:
-----------------
#include< stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100
int main(void){
int matrix1 [ROWS] [COLS];
int matrix2 [ROWS] [COLS];
int ** realMatrix;
int ** dummyMatrix;
int ** swapMatrix;


这些指针都是错误的类型。


matrix1和matrix2都是{ROWS / / COLS / ints数组的数组} 。

matrix1 [0]的类型是{array of / COLS / ints}。如果

int的大小是4个字节,那么matrix1 [10]的大小是COLS x 4 == 200个字节。


realMatrix等。,都是类型{指向指向int的指针}。

realMatrix [0]的类型是{指向int的指针},它具有任何大小

a指向int可能有(*非常*不太可能是200字节)。

int i;

realMatrix = matrix1; / *第15行* /
dummyMatrix = matrix2; / *第16行* /


这里遇到了麻烦。首先,没有C实现将

让你这样做而不至少抱怨它。你需要

投下它(但不要)。完成这些任务后,尝试使用realMatrix [n]或realMatrix [n] [m]访问
将会给你

未定义的行为。因为realMatrix [n]认为它正在取消引用一个指向int的指针的

指针,实际上它正在取消引用一个

转换指针到/ array / int。


如果你将realMatrix等声明为:


int(* realMatrix)[COLS]; / *指向COLS int数组的指针。 * /


事情更有可能按照你期望的方式行事,

虽然,因为你没有发布有效的实际代码有了这些

矩阵,我肯定不知道。

for(i = 0; i< MAXITERS; i ++){
/ *使用Matrix * /
/*...*/
/ *交换矩阵* /
swapMatrix = realMatrix;
realMatrix = dummyMatrix;
dummyMatrix = swapMatrix;
}

返回0;
}
Hi,
I''m having some problems working with two-dimensional arrays. Here''s
the story.
I got two 50x50 matrixes (is that the plural of matrix?) defined at
compile time.
No, the correct spelling would be matrices.
I want to work with them inside a for loop, but depending on the
iteration, one is the real one and the other is dummy. At the end of
the iteration I swap them.
I''m working with pointers but it is''nt working as assignments in lines
15 and 16 are from different types. I''m pretty sure I''ve been through
this before but can''t remember how I solved it. Any help? Here''s the
code I''ve got:
-----------------
#include <stdio.h>

#define ROWS 50
#define COLS 50
#define MAXITERS 100

int main(void){
int matrix1[ROWS][COLS];
int matrix2[ROWS][COLS];
int **realMatrix;
int **dummyMatrix;
int **swapMatrix;
These pointers are all the wrong type.

matrix1 and matrix2 are both {arrays of /ROWS/ array of /COLS/ ints}.
The type of matrix1[0] is {array of /COLS/ ints}. If the size of an
int is 4 bytes, then the size of matrix1[10] is COLS x 4 == 200 bytes.

realMatrix, etc., are all of type {pointer to pointer to int}.
The type of realMatrix[0] is {pointer to int}, which has whatever size
a pointer to int might have (*very* unlikely to be 200 bytes).
int i;

realMatrix=matrix1; /*Line 15*/
dummyMatrix=matrix2; /*Line 16*/
And here you run into trouble. First of all, no C implementation will
let you do this without at least complaining about it. You''d need to
cast it (but don''t). After making these assignments, then trying to
access something using realMatrix[n] or realMatrix[n][m] will give you
undefined behavior. Because realMatrix[n] thinks it''s dereferencing a
pointer to a pointer to int, when in reality it''s dereferencing a
converted pointer to /array/ of int.

If you declare realMatrix, etc, as:

int (*realMatrix)[COLS]; /* pointer to array of COLS ints. */

things are much more likely to behave the way you expect them to,
although, since you didn''t post the actual code that works with these
matrices, I can''t know that for certain.

for (i=0; i < MAXITERS; i++){
/*Work with Matrix*/
/*...*/
/*Swap Matrix*/
swapMatrix=realMatrix;
realMatrix=dummyMatrix;
dummyMatrix=swapMatrix;
}

return 0;
}



这篇关于二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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