指向数组 C++ 的指针
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10252837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pointer to array c++
提问by Sam Adamsh
What is the following code doing?
下面的代码在做什么?
int g[] = {9,8};
int (*j) = g;
From my understanding its creating a pointer to an array of 2 ints. But then why does this work:
根据我的理解,它创建了一个指向 2 个整数数组的指针。但是为什么这会起作用:
int x = j[0];
and this not work:
这不起作用:
int x = (*j)[0];
回答by Ben Cottrell
The parenthesis are superfluous in your example. The pointer doesn't care whether there's an array involved - it only knows that its pointing to an int
在您的示例中括号是多余的。指针不关心是否涉及数组 - 它只知道它指向一个int
int g[] = {9,8};
int (*j) = g;
could also be rewritten as
也可以改写为
int g[] = {9,8};
int *j = g;
which could also be rewritten as
也可以改写为
int g[] = {9,8};
int *j = &g[0];
a pointer-to-an-array would look like
指向数组的指针看起来像
int g[] = {9,8};
int (*j)[2] = &g;
//Dereference 'j' and access array element zero
int n = (*j)[0];
There's a good read on pointer declarations (and how to grok them) at this link here: http://www.codeproject.com/Articles/7042/How-to-interpret-complex-C-C-declarations
在此链接中对指针声明(以及如何理解它们)有很好的阅读:http: //www.codeproject.com/Articles/7042/How-to-interpret-complex-CC-declarations
回答by bames53
int g[] = {9,8};
This declares an object of type int[2], and initializes its elements to {9,8}
这声明了一个 int[2] 类型的对象,并将其元素初始化为 {9,8}
int (*j) = g;
This declares an object of type int *, and initializes it with a pointer to the first element of g.
这声明了一个 int * 类型的对象,并使用指向 g 的第一个元素的指针对其进行初始化。
The fact that the second declaration initializes j with something other than g is pretty strange. C and C++ just have these weird rules about arrays, and this is one of them. Here the expression g
is implicitly converted from an lvalue referring to the object g into an rvalue of type int*
that points at the first element of g.
第二个声明用 g 以外的东西初始化 j 的事实非常奇怪。C 和 C++ 只是有这些关于数组的奇怪规则,这就是其中之一。这里表达式g
从引用对象 g 的左值隐式转换为指向 gint*
的第一个元素的类型的右值。
This conversion happens in several places. In fact it occurs when you do g[0]
. The array index operator doesn't actually work on arrays, only on pointers. So the statement int x = j[0];
works because g[0]
happens to do that same implicit conversion that was done when j
was initialized.
这种转换发生在几个地方。事实上,当你这样做时它就会发生g[0]
。数组索引运算符实际上不适用于数组,仅适用于指针。所以该语句int x = j[0];
有效,因为g[0]
碰巧进行了与j
初始化时相同的隐式转换。
A pointer to an array is declared like this
一个指向数组的指针是这样声明的
int (*k)[2];
and you're exactly right about how this would be used
你对如何使用它是完全正确的
int x = (*k)[0];
(note how "declaration follows use", i.e. the syntax for declaringa variable of a type mimics the syntax for usinga variable of that type.)
(注意“声明遵循使用”的方式,即声明类型变量的语法模仿使用该类型变量的语法。)
However one doesn't typically use a pointer to an array. The whole purpose of the special rules around arrays is so that you can use a pointer to an array element as though it were an array. So idiomatic C generally doesn't care that arrays and pointers aren't the same thing, and the rules prevent you from doing much of anything useful directly with arrays. (for example you can't copy an array like: int g[2] = {1,2}; int h[2]; h = g;
)
但是,通常不使用指向数组的指针。围绕数组的特殊规则的全部目的是让您可以像使用数组一样使用指向数组元素的指针。所以惯用的 C 通常不关心数组和指针不是一回事,并且规则阻止您直接使用数组做任何有用的事情。(例如,你不能复制的阵列,如:int g[2] = {1,2}; int h[2]; h = g;
)
Examples:
例子:
void foo(int c[10]); // looks like we're taking an array by value.
// Wrong, the parameter type is 'adjusted' to be int*
int bar[3] = {1,2};
foo(bar); // compile error due to wrong types (int[3] vs. int[10])?
// No, compiles fine but you'll probably get undefined behavior at runtime
// if you want type checking, you can pass arrays by reference (or just use std::array):
void foo2(int (&c)[10]); // paramater type isn't 'adjusted'
foo2(bar); // compiler error, cannot convert int[3] to int (&)[10]
int baz()[10]; // returning an array by value?
// No, return types are prohibited from being an array.
int g[2] = {1,2};
int h[2] = g; // initializing the array? No, initializing an array requires {} syntax
h = g; // copying an array? No, assigning to arrays is prohibited
Because arrays are so inconsistent with the other types in C and C++ you should just avoid them. C++ has std::array
that is much more consistent and you should use it when you need statically sized arrays. If you need dynamically sized arrays your first option is std::vector.
因为数组与 C 和 C++ 中的其他类型非常不一致,所以你应该避免它们。C++ 具有std::array
更加一致的特性,当您需要静态大小的数组时应该使用它。如果您需要动态大小的数组,您的第一个选择是 std::vector。
回答by Luchian Grigore
j[0];
dereferences a pointer to int
, so its type is int
.
j[0];
取消引用指向 的指针int
,因此其类型为int
。
(*j)[0]
has no type. *j
dereferences a pointer to an int
, so it returns an int
, and (*j)[0]
attempts to dereference an int
. It's like attempting int x = 8; x[0];
.
(*j)[0]
没有类型。*j
取消引用指向 an 的指针int
,因此它返回 an int
,并(*j)[0]
尝试取消引用 an int
。这就像尝试int x = 8; x[0];
.