//defines an array of 280 pointers (1120 or 2240 bytes)
int  *pointer1 [280];

//defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
int (*pointer2)[280];      //pointer to an array of 280 integers
int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers

Using pointer2 or pointer3 produce the same binary except manipulations as ++pointer2 as pointed out by WhozCraig.

I recommend using typedef (producing same binary code as above pointer3)

typedef int myType[100][280];
myType *pointer3;

Note: Since C++11, you can also use keyword using instead of typedef

using myType = int[100][280];
myType *pointer3;

in your example:

myType *pointer;                // pointer creation
pointer = &tab1;                // assignation
(*pointer)[5][12] = 517;        // set (write)
int myint = (*pointer)[5][12];  // get (read)

Note: If the array tab1 is used within a function body => this array will be placed within the call stack memory. But the stack size is limited. Using arrays bigger than the free memory stack produces a stack overflow crash.

The full snippet is online-compilable at gcc.godbolt.org

int main()
{
    //defines an array of 280 pointers (1120 or 2240 bytes)
    int  *pointer1 [280];
    static_assert( sizeof(pointer1) == 2240, "" );

    //defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
    int (*pointer2)[280];      //pointer to an array of 280 integers
    int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers  
    static_assert( sizeof(pointer2) == 8, "" );
    static_assert( sizeof(pointer3) == 8, "" );

    // Use 'typedef' (or 'using' if you use a modern C++ compiler)
    typedef int myType[100][280];
    //using myType = int[100][280];
    
    int tab1[100][280];
    
    myType *pointer;                // pointer creation
    pointer = &tab1;                // assignation
    (*pointer)[5][12] = 517;        // set (write)
    int myint = (*pointer)[5][12];  // get (read)
  
    return myint;
}
Answer from olibre on Stack Overflow
Top answer
1 of 4
48
//defines an array of 280 pointers (1120 or 2240 bytes)
int  *pointer1 [280];

//defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
int (*pointer2)[280];      //pointer to an array of 280 integers
int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers

Using pointer2 or pointer3 produce the same binary except manipulations as ++pointer2 as pointed out by WhozCraig.

I recommend using typedef (producing same binary code as above pointer3)

typedef int myType[100][280];
myType *pointer3;

Note: Since C++11, you can also use keyword using instead of typedef

using myType = int[100][280];
myType *pointer3;

in your example:

myType *pointer;                // pointer creation
pointer = &tab1;                // assignation
(*pointer)[5][12] = 517;        // set (write)
int myint = (*pointer)[5][12];  // get (read)

Note: If the array tab1 is used within a function body => this array will be placed within the call stack memory. But the stack size is limited. Using arrays bigger than the free memory stack produces a stack overflow crash.

The full snippet is online-compilable at gcc.godbolt.org

int main()
{
    //defines an array of 280 pointers (1120 or 2240 bytes)
    int  *pointer1 [280];
    static_assert( sizeof(pointer1) == 2240, "" );

    //defines a pointer (4 or 8 bytes depending on 32/64 bits platform)
    int (*pointer2)[280];      //pointer to an array of 280 integers
    int (*pointer3)[100][280]; //pointer to an 2D array of 100*280 integers  
    static_assert( sizeof(pointer2) == 8, "" );
    static_assert( sizeof(pointer3) == 8, "" );

    // Use 'typedef' (or 'using' if you use a modern C++ compiler)
    typedef int myType[100][280];
    //using myType = int[100][280];
    
    int tab1[100][280];
    
    myType *pointer;                // pointer creation
    pointer = &tab1;                // assignation
    (*pointer)[5][12] = 517;        // set (write)
    int myint = (*pointer)[5][12];  // get (read)
  
    return myint;
}
2 of 4
14

Both your examples are equivalent. However, the first one is less obvious and more "hacky", while the second one clearly states your intention.

int (*pointer)[280];
pointer = tab1;

pointer points to an 1D array of 280 integers. In your assignment, you actually assign the first row of tab1. This works since you can implicitly cast arrays to pointers (to the first element).

When you are using pointer[5][12], C treats pointer as an array of arrays (pointer[5] is of type int[280]), so there is another implicit cast here (at least semantically).

In your second example, you explicitly create a pointer to a 2D array:

int (*pointer)[100][280];
pointer = &tab1;

The semantics are clearer here: *pointer is a 2D array, so you need to access it using (*pointer)[i][j].

Both solutions use the same amount of memory (1 pointer) and will most likely run equally fast. Under the hood, both pointers will even point to the same memory location (the first element of the tab1 array), and it is possible that your compiler will even generate the same code.

The first solution is "more advanced" since one needs quite a deep understanding on how arrays and pointers work in C to understand what is going on. The second one is more explicit.

๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ difference between pointer to pointer and 2d array
r/C_Programming on Reddit: Difference between pointer to pointer and 2d array
August 12, 2023 -

I know that I can go from one of the types 'char [1]' and 'char *' to the other. However, it seems not as easy with 'char [1][1]' and 'char **'.

In my main function I have

`char a[1][1];`

`a[0][0] = 'q';`

`printf("a: %p\n", a);`

`printf("*a: %p\n", *a);`

`printf("**a: %p\n", **a);`

I'm of course compiling with warnings and I know that gcc complains about the 5'th line as **a is actually of type char and not a pointer type. However running the code shows that 'a' and '*a' are actually the same pointer but '**a' is as expected something else (0x71 which I assume is related to 'q' in some way).

I'm trying to make sense of this and it seems that because *a and a are equal **a must also be equal to a because **a=*(*a)=*(a)=*a=a. It seems that the only error in this reasoning can be the types of a, *a, **a.

How is 'a' actually stored in memory? If 'a' is a pointer to another memory location (in my case 0x7fff9841f250) then surely *a should be the value at that memory address which in my case is also 0x7fff9841f250. So then **a would be the value at 0x7fff9841f250 which is the same value as 'a'... It seems that I cannot view the 2d array 'char a[1][1]' as pointers in a way that makes sense. But then how can I think of this type? What is the type and what does a, *a, **a, a[0], a[0][0] actually mean?

Can anyone help me make sense of this?

Discussions

Pointers and 2D arrays in C
If array[i][j] == *(array + i + j), array[0][1] would be always equal to array[1][0]. Also, the operator [] can be used on a pointer to get the value at that offset, but if you want to use it two times, you would need a double pointer. More on reddit.com
๐ŸŒ r/learnprogramming
8
1
October 6, 2021
How to pass a 2D array by pointer in C? - Stack Overflow
You need to change the definition of printarray if you want to pass it a 2D array: ... could you explain what the syntax "char *array[50]" means in plain english? 2013-05-23T21:43:41.123Z+00:00 ... Running cdecl explain 'char (*arr)[50]' yields declare arr as pointer to array 50 of char. More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Create a pointer to two-dimensional array - Stack Overflow
I don't think access beyond 10 ... anything indicating that. 2016-11-26T23:11:42.833Z+00:00 ... This answer seems to suggest that without the keyword static, the array would not be passed by reference, which is not true. Arrays are passed by reference anyway. The original question asked about a different use case - accessing elements of 2D array using a supplementary pointer within the ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Pointer to 2D Array - Stack Overflow
That is the type of expression *(gPtr1 + 3) is int[3] and there are only three such elements in the oridinal arrays. ... In this case using the pointers the arrays are interpretated as one-dimensional arrays with 9 elements and expressions More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ c programming
How to use pointers with 2D arrays in C?
March 10, 2024 - The syntax for declaring a 2D array in C is as follows: ... While array notation is straightforward and familiar to most, pointer notation offers a more powerful and flexible way to interact with arrays and their elements.
๐ŸŒ
Log2Base2
log2base2.com โ€บ C โ€บ pointer โ€บ 2d-array-and-pointers-in-c.html
2d array and pointers in c
... Since *arr holds the address of the first element, **arr will give the value stored in the first element. If we move **arr by 1 position(**arr+1), the value will be incremented by 1. If the array first element is 10, **arr+1 will be 11. 1. &arr is a 2D array pointer (int*)[row][col]. So, &arr+1 ...
๐ŸŒ
Medium
medium.com โ€บ @codewithcoders96 โ€บ pointers-and-multidimensional-arrays-in-c-c-137dede5b556
Pointers and Multidimensional Arrays in C/C++ | by Codewithcoders | Medium
August 21, 2024 - Pointers provide powerful capabilities for direct memory access, dynamic memory management, and the creation of complex data structures like linked lists and trees. int a = 10; int *ptr = &a; // 'ptr' now holds the address of 'a' An array is a collection of elements of the same data type stored in contiguous memory locations. Arrays in C/C++ can be single-dimensional (1D) or multidimensional (2D, 3D, etc.).
๐ŸŒ
OverIQ
overiq.com โ€บ c-programming-101 โ€บ pointers-and-2-d-arrays
Pointers and 2-D arrays - C Programming Tutorial - OverIQ.com
The important thing to notice is although parr and *parr points to the same address, but parr's base type is a pointer to an array of 5 integers, while *parr base type is a pointer to int. This is an important concept and will be used to access the elements of a 2-D array.
๐ŸŒ
Dyclassroom
dyclassroom.com โ€บ c โ€บ c-pointers-and-two-dimensional-array
C - Pointers and Two Dimensional Array - C Programming - dyclassroom | Have fun learning :-)
We will assign the address of the first element of the array num to the pointer ptr using the address of & operator. ... The two dimensional array num will be saved as a continuous block in the memory.
Find elsewhere
๐ŸŒ
Carnegie Mellon University
cs.cmu.edu โ€บ ~ab โ€บ 15-123S09 โ€บ lectures โ€บ Lecture 06 - Pointer to a pointer.pdf pdf
Lecture 06 2D Arrays & pointer to a ...
2-D arrays are represented as a ยท contiguous block of n blocks each with size m (i.e. can ยท hold m integers(or any data type) in each block). The ... A[1], A[2] etc.. We can access array elements using [ ] operator as A[i] or ยท using pointer operator *(A+i).
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ explain-pointers-and-two-dimensional-array-in-c-language
Pointers and Multidimensional Arrays in C
The compiler will allocate the memory for the above 2D array in a rowโˆ’wise order. Assuming that the first element of the array is at the address 1000 and the size of type "int" is 4 bytes, the elements of the array will get the following allocated memory locations โˆ’ ยท We will assign the address of the first element of the array num to the pointer ptr using the address of & operator.
๐ŸŒ
Aticleworld
aticleworld.com โ€บ home โ€บ how to access two dimensional array using pointers in c
How to access two dimensional array using pointers in C - Aticleworld
May 16, 2020 - After the creation of a new type for the 2d array, create a pointer to the 2d array and assign the address of the 2d array to the pointer.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ pointers and 2d arrays in c
r/learnprogramming on Reddit: Pointers and 2D arrays in C
October 6, 2021 -

I'm watching CS50 and they've introduced the concept of pointers. They've mostly referred to them to explain how strings work in C.

However, a pointer is just a variable containing an address to another variable. Instead of declaring an array as:

int numbers[SIZE];

I could do something like:

int *numbers = malloc(sizeof(int) * SIZE));

And instead of accessing a value within the array via numbers[4], I could say *(numbers + 4), where 4 (or any number for that matter) would be the address offset, and I'd be dereferencing it via the * operator.

What happens when I have a 2D array? My intuition would be as follows:

int array[h][w];

Is equivalent to:

int *array = malloc (sizeof(int) * h * w);

and referring to a specific value within the array would be:

*(array + h + w)

to access any value at array[h][w]. Am I right? Otherwise, what's the way of declaring 2D arrays using pointers? and how can I access a specific location?

Top answer
1 of 4
2
If array[i][j] == *(array + i + j), array[0][1] would be always equal to array[1][0]. Also, the operator [] can be used on a pointer to get the value at that offset, but if you want to use it two times, you would need a double pointer.
2 of 4
1
Think about it a little more. If you have a 2D array with 10x10 elements, that's 100 elements. Where in those elements is element (5, 5) (that is, the one accessed as array[5][5])? Is it at position 5+5 in the 100 elements? If so, is element (9, 9) at position 9+9? It isn't, is it? Rather, elements (0, 0) - (0, 9) are consecutive at positions 0-9, as expected; but then elements (1, 0) - (1, 9) come after that, at positions 10-19. Then follow elements (2, 0) - (2, 9) at positions 20-29, and so on. In other words, the position of an element (x, y) is x*width + y, where width is the size of the first dimension, or 10 in this case. You can't dynamically allocate a 2D array with arbitrary dimensions, since the compiler won't know how to produce the code to calculate offsets inside it. It needs to know that at compile time. What you can do is allocate an array where the inner dimension is fixed, but the outer dimension varies. In other words, a variable-length array of fixed-size arrays. For example, you can have a pointer to an array of 10 ints like this: int (*p)[10];, and then allocate an array of such ten-element arrays like this: p = malloc(sizeof(*p) * 99);. Now you can access one of the 99 arrays with p[x], and an element of that array with p[x][y]. If you want both dimensions to be arbitrary, you can allocate an array of pointers, and then set them to point to separate arrays. So you can declare an int **p;, allocate a pointer array with p = malloc(sizeof(*p) * 50);, and then go through p[x] for x = 0โ€“49 and allocate int arrays for them with p[x] = malloc(sizeof(int) * 20); or whatever. After that, you can access the elements as p[x][y] as though it were all one big array, but it's actually first indexing the pointer array, and then indexing one of the int arrays.
Top answer
1 of 6
43

char ** doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray if you want to pass it a 2D array:

void printarray( char (*array)[50], int SIZE )

or equivalently:

void printarray( char array[][50], int SIZE )
2 of 6
12

In main(), the variable "array" is declared as

char array[50][50];

This is a 2500 byte piece of data. When main()'s "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.

Yet in function printarray(), you declare

 char **array

"array" here is a pointer to a char *pointer.

@Lucus suggestion of void printarray( char array[][50], int SIZE ) works, except that it is not generic in that your SIZE parameter must be 50.

Idea: defeat (yeech) the type of parameter array in printarray()

void printarray(void *array, int SIZE ){
    int i;
    int j;
    char *charArray = (char *) array;

    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", charArray[j*SIZE + i] );
        }
        printf( "\n" );
    }
}

A more elegant solution is to make the "array" in main() an array of pointers.

// Your original printarray()
void printarray(char **array, int SIZE ){
    int i;
    int j;
    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", array[j][i] );
        }
        printf( "\n" );
    }
}

// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
  // Note: cleaner alternative syntax
  // array = malloc(sizeof *array * SIZE);
// Allocate rows
for (int row = 0; row<SIZE; row++) {
  // Note: sizeof(char) is 1. (@Carl Norum)
  // Shown here to help show difference between this malloc() and the above one.
  array[row] = (char *) malloc(SIZE * sizeof(char));
    // Note: cleaner alternative syntax
    // array[row] = malloc(sizeof(**array) * SIZE);
  }
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
  for (int col = 0; col<SIZE; col++) {
    array[row][col] = 'a';  // or whatever value you want
  }
}
// Print it
printarray(array, SIZE);
...
๐ŸŒ
Iditect
iditect.com โ€บ guide โ€บ cprogramming โ€บ c-2d-array-pointer.html
C Programming Language Two-dimensional Array Pointer (pointer To Two-dimensional Array)
... #include <stdio.h> int main() { int twoDArray[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // Declare and initialize a pointer to a 2D array int (*ptr)[4] = twoDArray; // Access elements using pointers printf("Value at (1,2): %d\n", ptr[1][2]); return 0; }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ pointer-array-array-pointer
Pointer to an Array | Array Pointer - GeeksforGeeks
The concept of pointer to an array can be extended to multidimensional arrays too: ... To define a pointer to a 2D array, both the number of rows and columns of the array must be specified in the pointer declaration.
Published: April 30, 2025
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ pointers-2d-arrays-c-gabriel-marques
Pointers and 2D-arrays in C
October 17, 2020 - When running, it simply prints the two arrays (1D and 2D), but at this point you should understand pointers and arrays :-) ./arrays_study Printing a 1D Array | 1 2 3 | Printing a 2D Array | 1 2 3 | | 4 5 6 | ... I hope this can be of any help. ... Mark contributions as unhelpful if you find them irrelevant or not valuable to the article.
๐ŸŒ
Quora
quora.com โ€บ How-we-can-declare-a-pointer-to-a-2D-array-in-the-C-language
How we can declare a pointer to a 2D array in the C language? - Quora
Answer (1 of 3): A simple pointer would suffice. Just address the pointer to a static/dynamic memory location. case 1: Static Allocation int array[10][20]; int *parray; parray = &array; e.g. - note that *(p+(i*3) +j) in the printf statement has (i*3) to manipulate the array location. The 3 h...
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 177473-passing-pointer-2d-array-function.html
passing a pointer to a 2d array to a function
April 28, 2019 - Inside the populate function, C ... stupid question time ... Hello cooper1200 Well first of all in order to pass a 2d array you must define in main 2 pointers (int** temp_array , you must also assign to variables (for example i and j) the size of rows and columns....
Top answer
1 of 10
163

Here you wanna make a pointer to the first element of the array

uint8_t (*matrix_ptr)[20] = l_matrix;

With typedef, this looks cleaner

typedef uint8_t array_of_20_uint8_t[20];
array_of_20_uint8_t *matrix_ptr = l_matrix;

Then you can enjoy life again :)

matrix_ptr[0][1] = ...;

Beware of the pointer/array world in C, much confusion is around this.


Edit

Reviewing some of the other answers here, because the comment fields are too short to do there. Multiple alternatives were proposed, but it wasn't shown how they behave. Here is how they do

uint8_t (*matrix_ptr)[][20] = l_matrix;

If you fix the error and add the address-of operator & like in the following snippet

uint8_t (*matrix_ptr)[][20] = &l_matrix;

Then that one creates a pointer to an incomplete array type of elements of type array of 20 uint8_t. Because the pointer is to an array of arrays, you have to access it with

(*matrix_ptr)[0][1] = ...;

And because it's a pointer to an incomplete array, you cannot do as a shortcut

matrix_ptr[0][0][1] = ...;

Because indexing requires the element type's size to be known (indexing implies an addition of an integer to the pointer, so it won't work with incomplete types). Note that this only works in C, because T[] and T[N] are compatible types. C++ does not have a concept of compatible types, and so it will reject that code, because T[] and T[10] are different types.


The following alternative doesn't work at all, because the element type of the array, when you view it as a one-dimensional array, is not uint8_t, but uint8_t[20]

uint8_t *matrix_ptr = l_matrix; // fail

The following is a good alternative

uint8_t (*matrix_ptr)[10][20] = &l_matrix;

You access it with

(*matrix_ptr)[0][1] = ...;
matrix_ptr[0][0][1] = ...; // also possible now

It has the benefit that it preserves the outer dimension's size. So you can apply sizeof on it

sizeof (*matrix_ptr) == sizeof(uint8_t) * 10 * 20

There is one other answer that makes use of the fact that items in an array are contiguously stored

uint8_t *matrix_ptr = l_matrix[0];

Now, that formally only allows you to access the elements of the first element of the two dimensional array. That is, the following condition hold

matrix_ptr[0] = ...; // valid
matrix_ptr[19] = ...; // valid

matrix_ptr[20] = ...; // undefined behavior
matrix_ptr[10*20-1] = ...; // undefined behavior

You will notice it probably works up to 10*20-1, but if you throw on alias analysis and other aggressive optimizations, some compiler could make an assumption that may break that code. Having said that, i've never encountered a compiler that fails on it (but then again, i've not used that technique in real code), and even the C FAQ has that technique contained (with a warning about its UB'ness), and if you cannot change the array type, this is a last option to save you :)

2 of 10
46

To fully understand this, you must grasp the following concepts:

Arrays are not pointers!

First of all (And it's been preached enough), arrays are not pointers. Instead, in most uses, they 'decay' to the address to their first element, which can be assigned to a pointer:

int a[] = {1, 2, 3};

int *p = a; // p now points to a[0]

I assume it works this way so that the array's contents can be accessed without copying all of them. That's just a behavior of array types and is not meant to imply that they are same thing.



Multidimensional arrays

Multidimensional arrays are just a way to 'partition' memory in a way that the compiler/machine can understand and operate on.

For instance, int a[4][3][5] = an array containing 435 (60) 'chunks' of integer-sized memory.

The advantage over using int a[4][3][5] vs plain int b[60] is that they're now 'partitioned' (Easier to work with their 'chunks', if needed), and the program can now perform bound checking.

In fact, int a[4][3][5] is stored exactly like int b[60] in memory - The only difference is that the compiler now manages it as if they're separate entities of certain sizes (Specifically, four groups of three groups of five).

{
  {1, 2, 3, 4, 5}
  {6, 7, 8, 9, 10}
  {11, 12, 13, 14, 15}
}
{
  {16, 17, 18, 19, 20}
  {21, 22, 23, 24, 25}
  {26, 27, 28, 29, 30}
}
{
  {31, 32, 33, 34, 35}
  {36, 37, 38, 39, 40}
  {41, 42, 43, 44, 45}
}
{
  {46, 47, 48, 49, 50}
  {51, 52, 53, 54, 55}
  {56, 57, 58, 59, 60}
}

From this, you can clearly see that each "partition" is just an array that the program keeps track of.



Syntax

Now, arrays are syntactically different from pointers. Specifically, this means the compiler/machine will treat them differently. This may seem like a no brainer, but take a look at this:

int a[3][3];

printf("%p %p", a, a[0]);

The above example prints the same memory address twice, like this:

0x7eb5a3b4 0x7eb5a3b4

However, only one can be assigned to a pointer so directly:

int *p1 = a[0]; // RIGHT !

int *p2 = a; // WRONG !

Why can't a be assigned to a pointer but a[0] can?

This, simply, is a consequence of multidimensional arrays, and I'll explain why:

At the level of 'a', we still see that we have another 'dimension' to look forward to. At the level of 'a[0]', however, we're already in the top dimension, so as far as the program is concerned we're just looking at a normal array.

You may be asking:

Why does it matter if the array is multidimensional in regards to making a pointer for it?

It's best to think this way:

A 'decay' from a multidimensional array is not just an address, but an address with partition data (AKA it still understands that its underlying data is made of other arrays), which consists of boundaries set by the array beyond the first dimension.

This 'partition' logic cannot exist within a pointer unless we specify it:

int a[4][5][95][8];

int (*p)[5][95][8];

p = a; // p = *a[0] // p = a+0

Otherwise, the meaning of the array's sorting properties are lost.

Also note the use of parenthesis around *p: int (*p)[5][95][8] - That's to specify that we're making a pointer with these bounds, not an array of pointers with these bounds: int *p[5][95][8]



Conclusion

Let's review:

  • Arrays decay to addresses if they have no other purpose in the used context
  • Multidimensional arrays are just arrays of arrays - Hence, the 'decayed' address will carry the burden of "I have sub dimensions"
  • Dimension data cannot exist in a pointer unless you give it to it.

In brief: multidimensional arrays decay to addresses that carry the ability to understand their contents.