# Thread: Casting from a 1D array pointer to a scalar pointer

1. ## Casting from a 1D array pointer to a scalar pointer

Hi guys,

I want to cast two pointers as follows:
(with the assumption that the memory has properly been allocated to both integer pointers)

insert
Code:
```/* if the data stored in a are as follows */

int (*a)[3] = {{2,1,3}, {3,4,7}};

/* then I want to cast to an int scalar pointer */

int *b = (int *) a;```
How are the data in b arranged ? (like this: {2, 1, 3, 3, 4, 7})

In contrast, if I want to cast from b back to a, how are the data in a then arranged?

Komkamol

2. I think you need to explain what you intend by this, since it is not legal C code, AFAIK:

Code:
`int (*a)[3] = {{2,1,3}, {3,4,7}};`
Anyway, if you assign a pointer to another variable, the memory is not copied. If the first variable was a pointer, you simply have two pointers to the same chunk of memory, so nothing is "rearranged".
Code:
```	int x = 25186;
char *y = (char*)&x;
printf("%c %c", y[0], y[1]);```
However, casting and type manipulation will affect how you access that memory. For example, the above, totally legal, code yields:

b b

Because a single int is (generally) 4 bytes, while a char is one, meaning four bytes fit into an int (and the first two bytes of the integer, 25186, are actually identical, as are the last two).

3. You are right that my code is illegal.

This "a" pointer must be allocated with a dynamic memory of 2*sizeof(int [3]).
I did not show this on my code. And I knew the code I wrote is illegal but I just wanted to tell
you what the member values stored in the "a" variable are, so that someone could give me
a concrete answer to this question with a concrete example.

insert
Code:
```int (*a)[3] = (int (*) [3])malloc(2*sizeof(int [3]));
a[0][0] = 2;
a[0][1] = 1;
a[0][2] = 3;
a[1][0] = 3;
a[1][1] = 4;
a[1][2] = 7;

int *b = (int *)malloc(6*sizeof(int));

b = (int *) a; /* what will happen at this position */```
Should the values accessed through "b" look like the following?:

b[0] equal to 2
b[1] equal to 1
b[2] equal to 3
b[3] equal to 3
b[4] equal to 4
b[5] equal to 7

In contrast, if I have "b" as shown above and want to cast the variable b in the other way around as follows:

insert
Code:
`a = (int (*) [3]) b;`
Should the values accessed through "a" look like the following?:

a[0][0] equal to 2
a[0][1] equal to 1
a[0][2] equal to 3
a[1][0] equal to 3
a[1][1] equal to 4
a[1][2] equal to 7

Komkamol

4. Originally Posted by komkamol
You are right that my code is illegal.

This "a" pointer must be allocated with a dynamic memory of 2*sizeof(int [3]).
I did not show this on my code. And I knew the code I wrote is illegal but I just wanted to tell
you what the member values stored in the "a" variable are, so that someone could give me
a concrete answer to this question with a concrete example.
It is more than just the fact that it is illegal; it does not make sense, either. Well, I presume it does to you. This is slightly better but you still use these mysterious parentheses (in red) in your declaration:
Code:
`int (*a)[3] = (int (*) [3])malloc(2*sizeof(int [3]));`
I think want you want should look more like this:
Code:
`int a[2][3];`
Now, if you want to dynamically allocate all of the memory, you are looking at something much more complex:
Code:
```#include <stdio.h>
#include <stdlib.h>

#define ROWS 2
#define COLS 3

int main(void) {
int **a = malloc(ROWS*sizeof(int*)),
nums[6] = { 2, 1, 3, 3, 4, 7 },
i, j, c=0;

for (i=0;i<ROWS;i++) {
a[i] = malloc(COLS*sizeof(int));
for (j=0; j<COLS; j++) a[i][j] = nums[c++];
}

printf("%d %d %d\n", a[0][1], a[0][0], a[1][0]);
return 0;
}```
Notice it is not necessary to (cast*)malloc in C, and your attempt to include a multiplier in your cast (the green part above) is non-sensical.

5. Originally Posted by MK27
This is slightly better but you still use these mysterious parentheses (in red) in your declaration:
That declares a pointer to an array of 3 ints, so they are not that mysterious.

6. Originally Posted by laserlight
That declares a pointer to an array of 3 ints, so they are not that mysterious.
Well I'll be darned. Sorry Komkamol. Still, I am almost positive that is not how you want to start, since an array of 3 ints cannot become a 2x3 matrix.

7. What
Code:
`b = (int *)a;`
does in your code is leak memory, specifically the memory you had allocated to b (the second malloc call).

If you want to copy things, you are looking for memcpy.

8. Originally Posted by komkamol
Code:
`b = (int *) a; /* what will happen at this position */`
This makes "b" point to the base address of "a" and the cast forces it to point to an int instead of an array of ints.
Originally Posted by komkamol
Code:
`int *b = (int *)malloc(6*sizeof(int));`
No need for the malloc() - leaks memory away.
Originally Posted by komkamol
Should the values accessed through "b" look like the following?:

b[0] equal to 2
b[1] equal to 1
b[2] equal to 3
b[3] equal to 3
b[4] equal to 4
b[5] equal to 7
Yep that is correct.
Originally Posted by komkamol
In contrast, if I have "b" as shown above and want to cast the variable b in the other way around as follows:

insert
Code:
`a = (int (*) [3]) b;`
Should the values accessed through "a" look like the following?:

a[0][0] equal to 2
a[0][1] equal to 1
a[0][2] equal to 3
a[1][0] equal to 3
a[1][1] equal to 4
a[1][2] equal to 7