Casting Pointer to Array (Int* to Int[2])

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

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.

Casting pointers to arrays in C++

Your variable pointer contains the address of the first character in string; while, pointerAddress is a pointer to a variable containing the address of the first character in string. Basically pointerAddress has nothing to do with string so

void *voidPointer = pointerAddress;
arrayPointer_type arrayPointer[3];
arrayPointer[0] = static_cast<arrayPointer_type>(voidPointer);
arrayPointer[1] = (arrayPointer_type) pointerAddress;
arrayPointer[2] = reinterpret_cast<arrayPointer_type>(pointerAddress);

is all wrong in that it is all casting pointerAddress which contains the address of some other variable. The address of string is the address of its first character i.e. try this

void* voidPointer = const_cast<char*>(pointer);
arrayPointer_type arrayPointer[3];
arrayPointer[0] = static_cast<arrayPointer_type>(voidPointer);
arrayPointer[1] = (arrayPointer_type)pointer;
arrayPointer[2] = reinterpret_cast<arrayPointer_type>(pointer);

Is there a way of casting a pointer to an array 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;

Casting int** to the pointer to a two-dimensional array of integers with fixed number of elements per column

  1. int** and int(*)[5] are different types (as n.m. pointed out)
  2. You may treat an array as a pointer, e.g. int a[5]; *(a+1) = 6;
  3. You may treat a pointer as an array, e.g. int *a = new int[5]; a[1] = 6;.

But treating object A as if it were an object B does not mean that it actually is object B.

What you can do though is declaring an int (*)[5], write the values of ptr_array_5by5 into it (after allocating memory, of course), and pass it to print2DArray_with5Columns.

On the other hand, yes there are casts that make your code compile. But I doubt that using one of them is getting you closer to your goal (see http://ideone.com/lVzNrN).

Pointer casting with unknown array size in C++

First I suggest to don't use runtime size for array in C/C++, except you using STL vector as an array. so instead of:

int i = 5;

you must use:

const int i = 5;

except you use Vector that is safe and better than intrinsic arrays.

how can I cast void pointer to a 2d array (array of pointers to arrays of ints), when I dont know array size at compile time? Is it somehow possible?

If we talk about C intrinsic array, It is not possible!

why it is not possible?
because C/C++ compiler not aware of your the array size, borders,.... so if you cast your 2d array to 1d array, it is possible. it is the reason that tab2 array can access to first 5th element of your array. really C/C++ compiler cannot distinguish the different of

int a[3][3]

with

int a[3*3]

so You must be aware of at least one dimension of your array:

int main() {
const int i = 3,j = 4;

int tab1[i][j] = {1,2,3,4,5,6,7,8,9,10,11};

//cast to void pointer
void *p = (void *)tab1;

auto a = (int (*)[i][12/i])p;
return 0;
}

In the above example, I aware about i and total count(12) and I calculate the second dimension.
I use auto keyword that very easily inferred the data type.



Related Topics



Leave a reply



Submit