Question: I am given a 1d buffer (`int buffer[100]`) of sufficient size. I need to re-organize the same memory as a 2d array. What i am trying to do is typecast it into a double pointer(`int ** p`) and then pass the pointer to the alloc function which will do the reorganizing. After the function returns, i need the statement `p[2][2]` to work.

This is the code:

```
#include <stdlib.h>
#include <stdio.h>

void alloc(int ** buf, int r, int c)
{
    int **temp;
    temp=buf;
    for(int i=0;i<r;i++)
        buf[i]=*(temp+i*c);
}
void main()
{
    int buffer[100];
    int **p=(int **)buffer;
    alloc(p, 4, 4);
    p[2][2]=10;
    printf("%d", p[2][2]);
}

```

The above code compiles successfully, but when i run the code i get an access violation upon execution of the statement 

```
p[2][2]=10;

```

I'm failing to understand where the problem lies. Is there a problem with the alloc function? Or is the problem somehwere else?

Answer: 
Let's examine your code to understand why it does not work.

Let's use a shorter buffer to understand what you are doing.

Let's say you have:

```
int buffer[9];

```

and you want to treat it like a 3x3 2D array. Memory used buffer:

```
buffer
|
v
+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+

```

You want to create an `int** p` such that:

```
p[0]        p[1]        [p2]
|           |           |
v           v           v
+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+

```

In order to accomplish that, you have allocate memory to store `p[0]`, `p[1]`, and `p[2]`. That means, you need:

```
int** p = new int*[3];
p[0] = buffer;
p[1] = buffer + 3;
p[2] = buffer + 6;

```

Now, `p` as well as `p[0]`, `p[1]`, `p[2]` point to valid addresses. You can use the syntax `p[m][n]`.

In our code, you just assigned `p` to point to the same location as `buffer` and by using the statement

```
buf[i]=*(temp+i*c);

```

you are just using the memory held by `buffer` as holders of `int*`, which not only tramples the data in `buffer` but also makes the values of the pointers unpredictable.

Here's a working program.

```
#include <stdio.h>

int main()
{
   int buffer[100];
   int rows = 4;
   int cols = 10;
   int **p = new int*[rows];
   for ( int i = 0; i < rows; ++i )
   {
      p[i] = buffer + i*cols;
      printf("%p\n", p[i]);
      for ( int j = 0; j < cols; ++j )
      {
         p[i][j] = i*2 + j*3;
      }
   }
   printf("\n");

   for ( int i = 0; i < rows; ++i )
   {
      for ( int j = 0; j < cols; ++j )
      {
         printf("%d ", p[i][j]);
      }
      printf("\n");
   }
}

```
If you are using C, you can simply cast your array to the correct type:

```
void main() {
    int width = 10, height = 10;
    int buffer[width*height];
    int (*p)[width] = (int (*)[width])buffer;
    p[2][2]=10;
    printf("%d", p[2][2]);
}

```

or directly use a 2D array:

```
void main() {
    int width = 10, height = 10;
    int buffer[width][height];
    buffer[2][2]=10;
    printf("%d", buffer[2][2]);
}

```

This works, because 2D arrays in C are arrays of arrays. Thus the 2D array is an array of ten arrays with ten elements each, all stored sequentially in memory. The type `int (*)[width]` denotes the type of a pointer to one of these line arrays. So, when you do `p[2]`, you invoke pointer arithmetic which adds twice the size of the line array to the pointer before dereferencing it. This (indirectly) results in an `int*` to the first element of the third line array, on which you can again invoke the same pointer arithmetic to select the right element in the 2D array.

This won't work as seamlessly in C++ since array types have to have a compile time size in C++. C is much more flexible in that respect, allowing you to easily pass a 2D array of arbitrary size to a function:

```
void foo(int width, int height, int (*image)[width]);

void main() {
    int width = 10, height = 10;
    int buffer[width][height];
    foo(width, height, buffer);
}

```