Home > Net >  2d array pointer used in function
2d array pointer used in function

Time:01-02

im writting a program in which i want to use this piece of code to transfer a 2d array in a function, but i dont understand fully how it works exactly. Can someone explain it, specifically line 7?


#include <stdio.h>
void print(int *arr, int m, int n)
{
    int i, j;
    for (i = 0; i < m; i  )
      for (j = 0; j < n; j  )
        printf("%d ", *((arr i*n)   j));
}
 
int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    int m = 3, n = 3;
 
    // We can also use "print(&arr[0][0], m, n);"
    print((int *)arr, m, n);
    return 0;
}

i also tried using

*( *(p   i)   j)

instead, but it didnt really work and i dont know why so if someone can explain why this didnt work as well i would really appreciate it.

CodePudding user response:

You pass a 2-dimesional Array to your print function, with the amount of items in the individual arrays and the amount of arrays in the 2D-Array.

Now let us come to the loop:

First of all if i and j are both zero you get the first Item of the first Arrays. In the next Iteration of the inner loop j is 1, thus (arr i*n) 1 points to 2 Element of the first Arrays, because i is still zero and j will be 1 ((arr 0 * 3) 1). In the next iteration it is the same but i is 2, thus pointing to the second element.

When the inner loop has finished i is increased to 1 and the expression is now (arr 1 * 3) 0. So now i * 3 will point to the first element of the second Array.

And in the third iteration of the outer loop i will point to the first element of the third array. So i * 3 is always the pointer to the first element of an array, in this 2D-Array and the j always points to an individual element in the Array. By combining this the 2D-Array gets printed.

*( *(p i) j) Does not work because, assuming p is an pointer to the array arr, because you are dereferencing it, so it in the first iteration it would evaluate to *(1 0) which results in a segmentation fault because you are not allowed to read this Memory Adress. This is, because by dereferencing it you are *(p 0) referring to the first Element of the first Array, which is 1.

CodePudding user response:

C is an extremely simple language, it became popular mainly because the simple parts were designed to be combined in ways that replaced complex parts of previous languages (see for as an example). One side effect is that it leaves out parts you expect in other languages.

Specifically for arrays, an array has no information on its size or format, it's assumed that the programmer will keep track of that, or that the size of every dimension but the first is constant (and normally the first one as well). So however many dimensions it's declared as, an array is just a single block of memory large enough to hold all elements, and the location is calculated internally using the [] operator.

Fun fact, C allows you to specify a[1] as 1[a], because it all translates to addition and multiplication. But don't do that.

In the event that you have an array that has variable sizes for more than one dimension, C doesn't support that so you have to do the math yourself, which is what that print() function is doing, where m and n are the sizes of the dimensions. The first row starts at arr (or arr 0), and goes to arr (n - 1) (0 to n-1 is n elements), and would look like arr[0][0] to arr[0][n-1] in a language that supported it. The next row starts at arr n (would be arr[1][0]) to arr (2 * n) - 1, and so on (up to what would be arr[m-1][n-1]).

In the function here, i and j go from 0 to m-1 and n-1 respectively, so you don't see - 1 in the code.

One more thing, C is at least helpful enough to know when you use on a pointer, you mean to increment by the size of the thing you're pointing to, so you don't have to figure out how many bytes in a int.

  • Related