33
votes

How do I cast or convert an int* into an 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]). 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 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.

2
First, why would you need that ? Hint: int* and int [2] have different purposes (though the second one can decay into the first).JBL
I am trying to simplify a file reading function. This is the concept I am grappling with in order to do said simplification.Josh C
Things never get simpler with pointers-to-arrays. Just use plain pointers and sizes.rodrigo
@rodrigo Things never get simpler with arrays. Just use std::vectorCaleth
@Caleth Indeed! That was exactly my conclusion in my answer below ;-).rodrigo

2 Answers

39
votes

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

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];

But a typedef will make it easier:

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.

0
votes

First of all casting pointer to C-style array it is a very bad practice in general.

But you can decay temporary array ("rvalue array") to address/pointer. 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.

P.S. Code listed above valid only for C99/C11 not for modern C++ compilers.