将指针转换为数组(int* 到 int[2]) [英] Casting pointer to Array (int* to int[2])

查看:68
本文介绍了将指针转换为数组(int* 到 int[2])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 int* 转换或转换为 int[x]?

How do I cast or convert an int* into an int[x]?

首先,我知道指针可以被索引.所以我知道我可以遍历指针和数组并手动转换指针.(例如,带有 arr[i] = p[i] 的 for 循环).我想知道是否可以用更少的代码行达到相同的结果.

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]). I want to know if the same result can be achieved in fewer lines of code.

作为一个例子,我试图将指针 int* c = new int[x] 转换为一个数组 int b[2]

As an example I tried to cast pointer int* c = new int[x] to an array 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];

这显然不起作用.

解决方案:不要这样做!如果必要创建一个指向数组的指针,然后指向该数组;对于我能理解的任何目的,这都是毫无意义的.如需更多详细信息,请参阅下面罗德里戈的回答.

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.

推荐答案

首先b是数组,不是指针,所以不可赋值.

First of all b is an array, not a pointer, so it is not assignable.

此外,您不能将任何内容强制转换为数组类型.但是,您可以转换为指向数组的指针.请注意,在 C 和 C++ 中,指向数组的指针相当不常见.使用普通指针或指向指针的指针并避免使用指向数组的指针几乎总是更好.

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.

不管怎样,你所要求的,或多或少是可以做到的:

Anyway, what you ask can be done, more or less:

int (*c)[2] = (int(*)[2])new int[2];

但是 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;

如果你只是为了好玩而做,那很好.在现实生活中,通常最好使用 std::vector.

Which is nice if you do it just for fun. For real life, it is usually better to use std::vector.

这篇关于将指针转换为数组(int* 到 int[2])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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