x数量为int的数组的指针是什么? [英] What is a pointer to an array of x amount of ints?

查看:47
本文介绍了x数量为int的数组的指针是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究 C ++ Primer ,感觉好像没有清楚的解释...

I've been working through C++ Primer, and I feel as if something hasn't been explained clearly...

我了解具有指向 int s的指针的数组的含义,例如:

I understand what it means to have a array of pointers to ints, for example:

int *foo [4];

int *foo1 [2][4];

但是,我不明白拥有指向一定数量 int s的 array 的指针的含义.这是书中的一个例子:

However, I don't understand what it means to have a pointer to an array of some number of ints. Here's an example from the book:

int ia[3][4];
int (*p) [4] = ia;

这里的 p 是什么,在1D和2D情况下这是什么意思?

What is p here, and what does this mean, both in the 1D and the 2D case?

推荐答案

当您声明类似

int array[137];

变量 array 的类型为 int [137] ;即137个 int 的数组.在C ++中,您可以创建指向任意类型的变量的指针或引用,因此可以创建指向变量 array 的指针.由于 array 的类型为"137个 int s的数组",因此指向 array 的指针将具有指向137个数组的指针"的指针.int s."像这样写出指针的语法是

the type of the variable array is int[137]; that is, an array of 137 ints. In C++, you can create pointers or references to variables of any type that you'd like, so you can create a pointer to the variable array. Since array has type "array of 137 ints," a pointer to array would have type "pointer to an array of 137 ints." The syntax for writing out a pointer like this would be

int (*arrayPtr)[137] = &array;

这被读为" arrayPtr 是一个指针,它指向的是137个 int s的数组".这里的语法很特殊,但是如果您想要一个指向137个整数的数组的指针,那将是您要做的.

This is read as "arrayPtr is a pointer, and what it points at is an array of 137 ints." The syntax here is unusual, but that's what you would do if you wanted a pointer to an array of 137 integers.

类似地,您可以像这样对 array 进行引用:

Similarly, you could make a reference to array like this:

int (&arrayRef)[137] = array;

看到指向实际在任何地方实际使用的数组的指针非常罕见-除了非常特殊的模板环境外,我从未见过这样做.对数组的引用有时在模板编程中使用,但否则我从未见过它们在任何地方使用过.

It's extremely uncommon to see pointers to arrays actually used anywhere - I've never seen it done except in very specialized template circumstances. References to arrays are sometimes used in template programming, but otherwise I've never seen them used anywhere before.

换句话说,很高兴知道它们的存在并知道如何阅读它们,但实际上,除非您开始进行一些相当高级的库开发或喜欢使用模板向导,否则您不太可能需要它们.

In other words, it's good to know these exist and know how to read them, but realistically you're unlikely to need them unless you start doing some pretty advanced library development or like to play around with template wizardry.

当您将数组到指针的衰减因素考虑进混合中时,这会变得很奇怪.您所拥有的示例代码基本上是

This gets weirder when you factor array-to-pointer decay into the mix. The example code you had was essentially

int myArray[137][42];
int (*arrayPtr)[42] = myArray;

arrayPtr 的类型是指向42个 int s数组的指针",其类型为 int(*)[42] .那么,为什么要为其分配类型为 int [137] [42] myArray ?这就是数组到指针衰减的作用.数组到指针衰减是一种隐式转换,它将数组转换为指向其第一个元素的指针.让我们看看在这里如何应用.

The type of arrayPtr is "pointer to an array of 42 ints", which is of type int (*) [42]. So why can we assign myArray, which has type int[137][42], to it? This is where array-to-pointer decay kicks in. Array-to-pointer decay is an implicit conversion that converts an array to a pointer to its first element. Let's see how that applies here.

myArray 是137个包含42个整数的数组的数组.可以说是由137个事物组成的数组,每个事物都是一个 int [42] ".这意味着当数组到指针的衰减应用于 myArray 时,它将转换为指向其第一个元素的指针.该元素本身是一个由42个整数组成的数组,因此应用数组到指针衰减的作用是将表达式 myArray 隐式转换为 int(*)[42] (指向一个由42个整数组成的数组的指针),具体地说,一个指针指向数组的第一行.

myArray is an array of 137 arrays of 42 integers. This can be though of as "an array of 137 things, each of which is an int[42]." This means that when array-to-pointer decay applies to myArray, it converts to a pointer to its first element. That element is itself an array of 42 integers, so the effect of applying the array-to-pointer decay is that the expression myArray implicitly converts to an int (*) [42] (a pointer to an array of 42 integers), specifically, one pointing at the first row in the array.

此分配的最终结果是, arrayPtr 现在指向 myArray 中的137个数组中的第一个.如果您问我,这简直是不可思议,我不建议编写这样的代码.我已经写了C ++多年了,从来没有遇到过在任何地方使用C ++的不幸,因此我认为可以将其归纳为只有库实现者才需要担心的怪异边缘案例".:-)

The net effect of this assignment is that arrayPtr now points to the first of the 137 arrays in myArray. This is just plain weird, if you ask me, and I would not advise writing code like this. I've been writing C++ for years and never had the misfortune of seeing this used anywhere, so I think it's safe to chalk this one up to "bizarre edge cases that only library implementers need to worry about." :-)

这篇关于x数量为int的数组的指针是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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