将二维数组分配表示为指针数学吗? [英] Representing a two-dimensional array assignment as a pointer math?

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

问题描述

我有一个数组,例如:

int array[3][5];

如果我想为该数组中的元素分配一个值,这很简单...例如:

If I want to assign a value to an element in that array, it's simple... ex:

array[1][2] = 8;

但是,我想用指针数学来表示它,这是正确的吗?

However, I want to represent it in a pointer math, would this be correct?

**(array + 5 * 1 + 2) = 8;

在两种情况下,它都在寻找第7个位置...我只是不确定是否需要在(代码)之外加上第2个*

In both cases it's looking for the 7th position... I'm just not sure if I need to include the 2nd * outside of (code)

**(code)

只需一点跟进.感谢所有为我提供帮助的人.我的困惑是由于讲师笔记中的错误而引起的,在我提供了您提供的信息后,他意识到了错误,并同意

Just a bit of a follow up. Thank you to everyone who was helping me with this. The confusion that I had came from a mistake in instructor's notes, and after I presented the information you've provided he realized the mistake and agrees that

*(*(array+row)+col) 

*(*array+MAX_COL*row+col) 

是二维数组的精确指针数学表示形式.

is the accurate pointer math representation for a two-dimensional array.

**(array+MAX_COL*row+col)类似于array[MAX_COL*row+col],用于多维数组的一维数组实现,就像对此帖子的其中一封回复中提到的 James Kanze 一样(第二个是比第一个快).

The **(array+MAX_COL*row+col) is like array[MAX_COL*row+col], which is used in single dimensional array implementation of multidimensional array, just like James Kanze mentioned in one of the replies to this post (the second one is faster than the first one).

推荐答案

array[1][2]等于*( *(array + 1) + 2).

这篇关于将二维数组分配表示为指针数学吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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