简体   繁体   English

将指针转换为数组(int* 到 int[2])

[英]Casting pointer to Array (int* to int[2])

How do I cast or convert an int* into an int[x]?如何将 int* 转换或转换为 int[x]?

First, I know that pointers can be indexed.首先,我知道指针可以被索引。 So I know that I can loop through the pointer and array and manually convert the pointer.所以我知道我可以循环遍历指针和数组并手动转换指针。 (eg. a for loop with arr[i] = p[i] ). (例如,带有arr[i] = p[i]的 for 循环)。 I want to know if the same result can be achieved in fewer lines of code.我想知道是否可以用更少的代码行来实现相同的结果。

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;

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 necessary create 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.有关更多详细信息,请参见下面罗德里戈的回答。

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.请注意,在 C 和 C++ 中,指向数组的指针相当少见。 It is almost always better to use plain pointers, or pointer-to-pointers and avoid pointer-to-arrays.使用普通指针或指向指针并避免指向数组的指针几乎总是更好。

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:但是typedef会更容易:

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

Though you can't reassign an array identifier.. sometimes the spirit of what you're doing allows you to simply create a reference and masquerade yourself as an array.尽管您无法重新分配数组标识符.. 有时您所做的事情的精神允许您简单地创建一个引用并将自己伪装成一个数组。 Note: this is just a slight extension of rodrigo's answer.注意:这只是罗德里戈答案的轻微延伸。

#include <iostream>

int main() {
    int a    = 1;
    int b[2] = { 2, 3 };
    int* c   = new int[3];
    
    c[0] = b[0];
    c[1] = b[1]; //3, this will be the start of our new array
    c[2] = a;
    // use c[1] as [0] in a new array of length 2
    // b = &c[1]; nope
    int (*c_aptr)[2] = (int(*)[2])(&c[1]); // create a pointer to an int[2]
    int (&b_2)[2] = *c_aptr; // create a reference from our pointer

    std::cout<<"b[1] = c[1] = " << c[1] << " = b_2[0] = " << b_2[0] << std::endl;
    std::cout<<"a    = c[2] = " << c[2] << " = b_2[1] = " << b_2[1] << std::endl;
}

output:输出:

b[1] = c[1] = 3 = b_2[0] = 3
a    = c[2] = 1 = b_2[1] = 1

First of all casting pointer to C-style array it is a very bad practice in general.首先,将指针转换为 C 风格的数组,这通常是一种非常糟糕的做法。

But you can decay temporary array ( "rvalue array" ) to address/pointer.但是您可以将临时数组( "rvalue array" )衰减为地址/指针。 Just write something like this只写这样的东西

#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.虽然我再次强调:当你这样做时,你应该有充分的理由进行这种技巧。

PS Code listed above valid only for C99/C11 not for modern C++ compilers.上面列出的PS代码仅对 C99/C11 有效,不适用于现代 C++ 编译器。

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. 请注意,这不是强制转换,但通常情况下,最简单的方法是执行您真正需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM