Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of casting a pointer to an array type?

Tags:

c++

c++11

C arrays are somewhat difficult to understand syntactically in C++ and can take some getting used to. Although a 1D array decays to a pointer:

void fn1(int x[2]) {}
void fn2(int*x) {}

fn1() and fn2() have the same function signature.

An array actually does have a type that includes how many elements are in the array. As in:

void fn(int (&)[2]) {}

fn() will only accept a 2 element int array.

Thing is, I can only see that the array of a fixed number of elements can only be generated by stack, file scope or struct/class allocation with that signature:

int twoElementArray[2];

If I were to dynamically allocate it on the heap, I can't seem to get the same signature. I thought that I might be able to cast it, but without success:

int (&array)[2] = reinterpret_cast<int(&)[2]>(new int[2]); // FAIL!

Any ideas as to how this might be accomplished if at all?

 


EDIT: Although I selected an answer, it actually doesn't actually cast anything, but uses a definitely better method then casting (better not to cast if not required IMO). However, it technically doesn't answer the question since the question asked if there's "a way of casting a pointer to an array type?" The answer is yes.

int (&array)[2] = *reinterpret_cast<int(*)[2]>(new int[2]); // SUCCESS!

Note that I don't necessarily recommend doing this, but it does answer the question. If I needed to convert a pointer to an array type though, that would be how to do it. Read the picked answer for a better solution when using operator new[].

like image 241
Adrian Avatar asked Jun 23 '13 07:06

Adrian


People also ask

Can you assign a pointer to an array?

You cannot assign a new pointer value to an array name. The array name will always point to the first element of the array.

How do I turn a pointer point into an array of elements?

To assign a pointer to the array we can use the following declaration... int a[10]; int *pa = &a[0]; Basically this assigns the memory address of a[0] the first element in array a to pointer of type int .

Can pointer be type casted?

You can cast a pointer to another pointer of the same IBM® i pointer type. Note: If the ILE C compiler detects a type mismatch in an expression, a compile time error occurs. An open (void) pointer can hold a pointer of any type.

Can you cast a pointer to an int?

Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.


2 Answers

If I understand your problem correctly, you'd want to do something like this:

// allocate an array of one int[2] dynamically
// and store a pointer to it
int(*p)[2] = new int[1][2];

// now initialize a reference to it
int(&array)[2] = *p;

// delete the array once you no longer need it
delete[] p;
like image 138
jrok Avatar answered Oct 25 '22 17:10

jrok


I think this is what you are looking for. For the heap, a two-dimensional array int[M][N] decays to int(*)[N]. To pass it by reference, dereference it (see m below):

#include <iostream>
using namespace std;

void func(int (&x)[2])
{
    cout << x[0] << ' ' << x[1] << endl;
}

int main()
{
    // on the heap
    auto m = new int[1][2];
    m[0][0] = 1; m[0][1] = 2;
    auto n = new int[1][3];
    n[0][0] = 4; n[0][1] = 5; n[0][2] = 6;

    // on the stack
    int o[2] = {7,8};
    int p[3] = {9,10};

    func(*m);
    //func(*n); // doesn't compile, wrong size
    func(o);
    //func(p); // doesn't compile, wrong size
}

Output:

1 2
7 8
like image 21
Mark Tolonen Avatar answered Oct 25 '22 16:10

Mark Tolonen