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[]
.
You cannot assign a new pointer value to an array name. The array name will always point to the first element of the array.
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 .
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.
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.
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;
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With