//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 oHo on Stack Overflow
🌐
Log2Base2
log2base2.com β€Ί C β€Ί pointer β€Ί 2d-array-and-pointers-in-c.html
2d array and pointers in c
&arr is a pointer to the entire 2D(3x3) array. i.e. (int*)[3][3] If we move &arr by 1 position(&arr+1), it will point to the next 2D block(3X3). In our case, the base address of the 2D array is 1000.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί c language β€Ί how-to-declare-a-two-dimensional-array-of-pointers-in-c
How to declare a Two Dimensional Array of pointers in C? - GeeksforGeeks
June 29, 2022 - Below is the implementation of the 2D array of pointers. ... #include <stdio.h> // Drivers code int main() { int arr1[5][5] = { { 0, 1, 2, 3, 4 }, { 2, 3, 4, 5, 6 }, { 4, 5, 6, 7, 8 }, { 5, 4, 3, 2, 6 }, { 2, 5, 4, 3, 1 } }; int* arr2[5][5]; // Initialising each element of the // pointer array with the address of // element present in the other array for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { arr2[i][j] = &arr1[i][j]; } } // Printing the array using // the array of pointers printf("The values are\n"); for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { printf("%d ", *arr2[i][j]); } printf("\n"); } return 0; }
🌐
Codedamn
codedamn.com β€Ί news β€Ί c programming
How to use pointers with 2D arrays in C?
March 10, 2024 - Pointers in C are variables that store memory addresses, acting as a direct link to the location of data within the memory. On the other hand, 2D arrays are essentially arrays of arrays, allowing for the storage of data in a tabular form.
Top answer
1 of 4
47
//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
13

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.

🌐
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. So, if we increment the value of ptr by 1 we will move to the next block in the allocated memory. In the following code we are printing the content of the num array using for loop and by incrementing the value of ptr. #include <stdio.h> int main(void) { // 2d array int num[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} }; // pointer ptr pointing at array num int *ptr = &num[0][0]; // other variables int ROWS = 3, COLS = 4, TOTAL_CELLS = ROWS * COLS, i; // print the elements of the array num via pointer ptr for (i = 0; i < TOTAL_CELLS; i++) { printf("%d ", *(ptr + i)); } return 0; }
🌐
OverIQ
overiq.com β€Ί c-programming-101 β€Ί pointers-and-2-d-arrays
Pointers and 2-D arrays - C Programming Tutorial - OverIQ.com
In the previous chapter, we have already discussed that the name of a 1-D array is a constant pointer to the 0th element. In the case, of a 2-D array, 0th element is a 1-D array. Hence in the above example, the type or base type of arr is a pointer to an array of 4 integers.
🌐
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 ...
address of a pointer. Just as 1D array of ints is thought Β· of as const int*, we can think of a 2D array as a const Β· int** Understanding Β· how Β· pointers Β· work Β· is Β· critical Β· to Β· understanding how to work with C language. Now we have Β· discussed two types of pointers, * and ** We can think of int* as an address of an int and int** as Β·
🌐
TutorialsPoint
tutorialspoint.com β€Ί explain-pointers-and-two-dimensional-array-in-c-language
Explain pointers and two-dimensional array in C language
Following is the C program for pointers and two-dimensional array βˆ’ Β· #include<stdio.h> main ( ){ int a[3] [3], i,j; int *p; clrscr ( ); printf ("Enter elements of 2D array"); for (i=0; i<3; i++){ for (j=0; j<3; j++){ scanf ("%d", &a[i] [j]); } } p = &a[0] [0]; printf ("elements of 2d array are"); for (i=0; i<3; i++){ for (j=0; j<3; j++){ printf ("%d \t", *(p+i*3+j)); } printf (" "); } getch ( ); }
Find elsewhere
🌐
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?

Top answer
1 of 5
21

data is a 2 dimentional array, which has 4 rows and each row has 3 elements (ie 4 X 3).

Now, Ptr = *data; means you are storing the starting address of 1st row to the pointer variable Ptr. This statement is equivalent to Ptr = *(data + 0). Ptr = *(data + 1) - this means we are assigning 2nd row's starting address.

Then *Ptr or *(Ptr + 0) will give you the value of the first element of the row to which is pointing. Similarly, *(Ptr + 1) will give you the value of the second element of the row.

The for loop in your program is used to identify which row has the maximum value of the sum of its elements (3 elements). Once the control comes out of that for loop, Ptr will be pointing to the row which has the maximum sum of its elements and sum0 will have the value of the sum.

Consider an array int a[5];, I hope you know that a[0] and 0[a] is the same. This is because a[0] means *(a+0) and 0[a] means *(0 + a). This same logic can be used in 2 dimensional array.

data[i][j] is similar to *(*(data + i) + j). We can write it as i[data][j] also.

For more details please refer to the book "Understanding Pointers in C" by Yashavant Kanetkar.

2 of 5
7

Ptr = *data; is short for *(data+0)+0 which is a pointer for first column element of the first row. the first 0 added with data is the row no., which is indirected and takes us to the first row. * (data+0) is still a address and not a value it points to (for 2D array). So, Ptr now points to the address of first column in first row. The second zero is the column no.. So, first row and first column's memory address is chosen. Using indirection (*) again would only now give value that the address holds. like * (*(data+0)+0) or **data.

Generally, if p is pointer name,i row number and j column number,

  1. (*(p+i)+j) would give a memory address of a element in 2D array. i is row no. and j is col no.,
  2. *(*(p+i)+j) would give the value of that element.
  3. *(p+i) would access the ith row
  4. to access columns, add column number to *(p+i). You may have to declare the pointer as (*p)[columns] instead of just *p. Doing so, you are declaring pointer to an 2D array.

Using pointer arithmetic is treating 2d array like 1D array. Initialize pointer *Ptr to first element (int *Ptr = *data) and then add an no. (Ptr + n) to access the columns. Adding a number higher than column number would simply continue counting the elements from first column of next row, if that exists.

🌐
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 - You can also say that acData is similar to the &acData[0] Below expression describe a relation between array and pointer, acData [i] = *(acData +i) β€”β€”β€”β€”β€”β€”β€”β€”->1D array in form of pointer a[i] = *(a+ i) β€”β€”β€”β€”β€”β€”β€”β€”->ith element of an 1D array acData [i][j] = *(acData [i]+j); ————————–>2D array in form of 1D array and pointer.
🌐
Cprogramming
cboard.cprogramming.com β€Ί c-programming β€Ί 177473-passing-pointer-2d-array-function.html
passing a pointer to a 2d array to a function
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
25

By the letter of the law, here's how to do it:

// Create 2D array of pointers:
int*** array2d = new (int**)[rows];
for (int i = 0; i < rows; ++i) {
  array2d[i] = new (int*)[cols];
}

// Null out the pointers contained in the array:
for (int i = 0; i < rows; ++i) {
  for (int j = 0; j < cols; ++j) {
    array2d[i][j] = NULL;
  }
}

Be careful to delete the contained pointers, the row arrays, and the column array all separately and in the correct order.

However, more frequently in C++ you'd create a class that internally managed a 1D array of pointers and overload the function call operator to provide 2D indexing. That way you're really have a contiguous array of pointers, rather than an array of arrays of pointers.

2 of 10
8

It depends. It can be as simple as:

int main()
{
    int*   data[10][20]; // Fixed size known at compile time

    data[2][3] = new int(4);
}

If you want dynamic sizes at runtime you need to do some work.
But Boost has you covered:

int main()
{
   int  x;
   int  y;
   getWidthAndHeight(x,y);

   // declare a 2D array of int*
   boost::multi_array<int*,2>   data(boost::extents[x][y]);

   data[2][3] = new int(6);
}

If you are fine with jagged arrays that can grow dynamically:

int main()
{
    std::vector<std::vector<int*> >   data;

    data.push_back(std::vector<int*>(10,NULL));
    data[0][3] = new int(7);
}

Note: In all the above. I assume that the array does not own the pointer. Thus it has not been doing any management on the pointers it contains (though for brevity I have been using new int() in the examples). To do memory management correctly you need to do some more work.

Top answer
1 of 10
162

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.

🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί pointers and 2d arrays in c
r/learnprogramming on Reddit: Pointers and 2D arrays in C
September 5, 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.
🌐
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...
🌐
TutorialsPoint
tutorialspoint.com β€Ί cprogramming β€Ί c_pointers_vs_multi_dimensional_arrays.htm
Pointers and Multidimensional Arrays in C
Since the value of the pointer increments by the size of the data type, "x++" moves the pointer to the next element in the array. #include <stdio.h> int main(){ int arr[] = {1, 2, 3, 4, 5}; int length = sizeof(arr) / sizeof(arr[0]); int i = 0; int *ptr = arr; while (i < length){ printf("arr[%d]: %d \n", i, *(ptr + i)); i++; } return 0; } When you run this code, it will produce the following output βˆ’ ... If a one-dimensional array is like a list of elements, a two-dimensional array is like a table or a matrix. The elements in a 2D array can be considered to be logically arranged in rows and columns.
🌐
Java Guides
javaguides.net β€Ί 2023 β€Ί 09 β€Ί c-program-to-access-two-dimensional-array-using-pointers.html
C Program to Access Two-Dimensional Array Using Pointers
September 12, 2023 - Using pointers to navigate and manipulate them can sometimes be tricky due to the way they are stored in memory. This guide will demonstrate how to access elements of a two-dimensional array using pointers. 1. Declare a two-dimensional array. 2. Use pointers to traverse this array. 3. Print elements of the array using pointer notation. #include <stdio.h> int main() { int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; int i, j; // Printing elements of 2D array using pointers for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { printf("%d ", *(*(arr + i) + j)); } printf("\n"); } return 0; } 1 2 3 4 5 6 7 8 9 Β·