如何将二维数组分配给**指针? [英] how to assign two dimensional array to **pointer ?

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

问题描述

如何将二维数组分配给**指针?这是我想做的事情的想法

how to assign two dimensional array to **pointer ? this is the idea of what i want to do

int arrray [2][3];
int **pointer = array;
so pointer[0][1]= 1;

所以有什么帮助吗?提前致谢

so any help ? thanks in advance

推荐答案

简单的答案是你不能.二维数组是保存每一行的连续内存块,而指向指针的指针可以引用一个内存位置,其中指向包含整数的不同内存位置的指针所在的位置.

The simple answer is that you cannot. A bidimensional array is a contiguous block of memory that holds each line, while a pointer to pointer can refer to a memory location where a pointer to a different memory location containing the integers is.

另一方面,您可以创建一个单独的数据结构,以您想要的方式保存指向元素的指针(即创建一个指针数组,将这些指针初始化为每行的开头,并使用指向该元素的指针)指针数组作为 pointer),但它根本没有用,反而会使一切不必要地复杂化.

You can on the other hand create a separate data structure that holds the pointers to the elements in the way you want (i.e. create an array of pointers, initialize those pointers to the beginning of each row, and use a pointer to that array of pointers as pointer), but it is not useful at all, but rather will complicate everything unneedingly.

问题可能来自于普遍的误解,即数组和指针是相同,而它们不是.数组可以衰减为指向数组第一个元素的指针(并且经常这样做),但该指针的类型是第一个元素的类型.在二维数组中,第一个元素的类型是内部数组,而不是基本元素类型.

The question probably comes from the common misconceptions that arrays and pointers are the same, which they are not. An array can decay to a pointer to the first element of the array (and will do so quite often), but the type of that pointer is the type of the first element. In a bidimensional array, the type of the first element is the inner array, not the basic element type.

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

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