C++ 将指针转换为 Array(int* 到 int[2])
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20046429/
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
Casting pointer to Array (int* to int[2])
提问by Josh C
How do I cast or convert an int* into an int[x]?
如何将 int* 转换或转换为 int[x]?
As an example I tried to cast pointer int* c = new int[x]
to an array int b[2]
作为一个例子,我试图将指针转换int* c = new int[x]
为一个数组int b[2]
int a = 1;
int b[2] = { 2, 3 };
int* c = new int[b[1]];
c[0] = b[0];
c[1] = b[1];
c[2] = a;
First of all, I know that I can loop through the pointerand array. Thus convert the pointer to an array with []
operators to index the pointer and array, assigning pointer elements to array elements as I iterate (eg. arr[i] = p[i]
). I want to know if the same result can be achieved in fewer lines of code.
首先,我知道我可以遍历指针和数组。因此,使用[]
运算符将指针转换为数组以索引指针和数组,在我迭代时将指针元素分配给数组元素(例如arr[i] = p[i]
)。我想知道是否可以用更少的代码行实现相同的结果。
I wanted to see what values were where, so I made a simple program to output addresses and values. The output is just below:
我想看看值在哪里,所以我做了一个简单的程序来输出地址和值。输出如下:
Address of {type: int} &a = 0031FEF4; a = 1
Address of {type: int[2]} &b = 0031FEE4; b = 0031FEE4
Address of {type: int[2]} &b[0] = 0031FEE4; b[0] = 2
Address of {type: int[2]} &b[1] = 0031FEE8; b[1] = 3
Address of {type: int*} &c = 0031FED8; c = 008428C8
Address of {type: int*} &c[0] = 008428C8; c[0] = 2
Address of {type: int*} &c[2] = 008428D0; c[2] = 1
Once I made sure I knew what was where I tried a few things. The first idea that came to mind was to get the address of the second element to the pointer's allocation, then replace the array's memory address with it (see the code just below). Everything I did try ultimately failed, usually with syntax errors.
一旦我确定我知道我在哪里尝试了一些东西。想到的第一个想法是获取指针分配的第二个元素的地址,然后用它替换数组的内存地址(参见下面的代码)。我所做的一切最终都失败了,通常是语法错误。
This is what I tried.I really want this to work, since it would be the simplest solution.
这是我尝试过的。我真的希望它起作用,因为这将是最简单的解决方案。
b = &c[1];
This did not work obviously.
这显然不起作用。
Edit: Solution:Don't do it!If it's necessarycreate a pointer to an array and then point to the array; this is pointless for any purposes I can fathom. For more detailed information see the answer by rodrigo below.
编辑:解决方案:不要这样做!如果有必要创建一个指向数组的指针,然后指向该数组;对于我能理解的任何目的,这都是毫无意义的。有关更多详细信息,请参阅下面罗德里戈的回答。
回答by rodrigo
First of all b
is an array, not a pointer, so it is not assignable.
首先b
是一个数组,而不是一个指针,所以它是不可赋值的。
Also, you cannot cast anything to an array type. You can, however, cast to pointer-to-array. Note that in C and C++ pointer-to-arrays are rather uncommon. It is almost always better to use plain pointers, or pointer-to-pointers and avoid pointer-to-arrays.
此外,您不能将任何内容强制转换为数组类型。但是,您可以转换为指向数组的指针。请注意,在 C 和 C++ 中,指向数组的指针相当不常见。使用普通指针或指向指针的指针并避免使用指向数组的指针几乎总是更好。
Anyway, what you ask can be done, more or less:
不管怎样,你问的都可以做到,或多或少:
int (*c)[2] = (int(*)[2])new int[2];
But a typedef
will make it easier:
但是 atypedef
会让事情变得更容易:
typedef int ai[2];
ai *c = (ai*)new int[2];
And to be safe, the delete should be done using the original type:
为了安全起见,删除应该使用原始类型:
delete [](int*)c;
Which is nice if you do it just for fun. For real life, it is usually better to use std::vector
.
如果您只是为了好玩而这样做,那很好。对于现实生活,通常最好使用std::vector
.
回答by Twissell
First of all casting pointer to C-style array it is a very badpractice in general.
首先,将指针转换为 C 风格的数组,这通常是一种非常糟糕的做法。
But you can decay temporary array ("rvalue array") to address/pointer. Just write something like this
但是您可以将临时数组("rvalue array")衰减为地址/指针。只写这样的东西
#include <stdio.h>
int main()
{
int *array = (int[2]){1, 2};
printf("%d", array[0]); /* array[0] contains 1 now */
return 0;
}
Although I emphase again: when you doing so, you should have good reasons for such kind of tricks.
虽然我再次强调:当你这样做时,你应该有充分的理由进行这种技巧。
P.S.Code listed above valid only for C99/C11 not for modern C++ compilers.
上面列出的PS代码仅对 C99/C11 有效,不适用于现代 C++ 编译器。
回答by crizCraig
To index an array via a pointer:
通过指针索引数组:
int* arr;
int x = arr[2]; // Pointers can be subscripted
Note, this is not casting, but often times is the simplest way to do what you really need.
请注意,这不是强制转换,但通常是执行您真正需要的操作的最简单方法。