从C/Objective C中的2D数组获取数据

时间:2021-10-11 13:12:28

It's quite a simple question, yet I cannot find an answer that works.

这是一个很简单的问题,但我找不到有效的答案。

Take the following 2D array: int grid[4][4]. I could fill it with integers, so that visually, it could look something like:

取以下2D数组:int grid[4][4]。我可以用整数来填充它,这样看起来,它可以是这样的:

1,0,0,5,0 0,0,0,6,7 3,0,0,0,7 2,0,0,0,9 4,0,0,2,0 (each segment a new row)

1 0 0 0 5 0 0 0 0 0 6 7 3 0 0 0 0 7 2 0 0 0 0 0 9 4 0 2 0 0 0 2 0(每一段都是一行)

Now, what if I only wanted the data of one row, to be given as a standard array? The way I would of thought to do this would be: grid[0], which would give me 1,0,0,5,0.

现在,如果我只想要一行的数据作为标准数组呢?我想这样做的方式是:网格[0],它会给我1 0 0 5 0。

However, this does not seem to work. Can anyone tell me how I can extract rows of data? Here is a code example

然而,这似乎行不通。谁能告诉我如何提取数据行吗?下面是一个代码示例

int grid[4][4];

//Add in some numbers, e.g.
grid[0][3] = 5;

//Get the first row
int* row1 = grid[0]; //I'm not sure about this

For instance, this example should set "row1" to "0,0,0,5,0"

例如,这个示例应该将“row1”设置为“0,0,0,0,5,0”

I have the feeling I'm doing something awfully wrong here...

我觉得我做错了什么…

There is no error message, but it just gives "row1" a random string of integers instead, which are definitely not correct.

没有错误消息,但它只是给了“row1”一个随机的整数字符串,这肯定是不正确的。

1 个解决方案

#1


5  

Remember, in an array declared int grid[4][4], you can only store four lots of four elements (sixteen total).

记住,在一个声明为int grid[4][4]的数组中,只能存储4个4个元素(总共16个)。

Your array, { 1,0,0,5,0 }, { 0,0,0,6,7 }, { 3,0,0,0,7 }, { 2,0,0,0,9 }, { 4,0,0,2,0 } is actually an int [5][5].

你的数组,{ 1 0 0 5 0 },{ 0,0,0,6、7 },{ 3 0 0 0 7 },{ 2 0 0 0 9 },{ 4 0 0 2 0 }实际上是一个int[5][5]。

What you're doing is not wrong; row1 will be a pointer to the first item of grid[0]. If you wanted to print each element, that's a good start...

你所做的并没有错;row1将是指向第一个网格的指针[0]。如果您想要打印每个元素,这是一个很好的开始。

In a moment of clarity, you'll likely realise when I mention the word "loop"... You then need a "loop" to loop through each element of the row!

当我提到“循环”这个词的时候,你可能会马上意识到……然后需要一个“循环”来循环遍历行中的每个元素!

int *row = grid[0];
for (size_t x = 0; x < sizeof grid[0] / sizeof grid[0][0]; x++) {
     printf("%d, ", row[x]);
}
putchar('\n');

#1


5  

Remember, in an array declared int grid[4][4], you can only store four lots of four elements (sixteen total).

记住,在一个声明为int grid[4][4]的数组中,只能存储4个4个元素(总共16个)。

Your array, { 1,0,0,5,0 }, { 0,0,0,6,7 }, { 3,0,0,0,7 }, { 2,0,0,0,9 }, { 4,0,0,2,0 } is actually an int [5][5].

你的数组,{ 1 0 0 5 0 },{ 0,0,0,6、7 },{ 3 0 0 0 7 },{ 2 0 0 0 9 },{ 4 0 0 2 0 }实际上是一个int[5][5]。

What you're doing is not wrong; row1 will be a pointer to the first item of grid[0]. If you wanted to print each element, that's a good start...

你所做的并没有错;row1将是指向第一个网格的指针[0]。如果您想要打印每个元素,这是一个很好的开始。

In a moment of clarity, you'll likely realise when I mention the word "loop"... You then need a "loop" to loop through each element of the row!

当我提到“循环”这个词的时候,你可能会马上意识到……然后需要一个“循环”来循环遍历行中的每个元素!

int *row = grid[0];
for (size_t x = 0; x < sizeof grid[0] / sizeof grid[0][0]; x++) {
     printf("%d, ", row[x]);
}
putchar('\n');