Luna
Luna

Reputation: 73

Multidimensional arrays understanding for c

I have a question regarding passing an array to another function.

#include <stdio.h>
void print (int ar[]);

main () {
    int array[2][2] = {1,2,3,4};
    print(array);
}

void print (int ar[]) {
    printf("%d\n", ar[0]);
    printf("%d\n", *(ar+1));
}

So let's say if i'm passing array to function print. Am I passing address of the first 1-D array (array[0]) or am i just passing the address of the very first variable (array[0][0]).

And as for the print function, in this case, am I correct to say that by printing ar[0], I am printing array[0][0]?

But why is it that for the case of printing *(ar+1), I am actually printing array[0][1] instead of array[1][0]?

Thank you everyone in advance!

Upvotes: 0

Views: 75

Answers (4)

John Bollinger
John Bollinger

Reputation: 180201

So let's say if [I]'m passing array to function print. Am I passing address of the first 1-D array (array[0]) or am [I] just passing the address of the very first variable (array[0][0]).

You are passing a pointer to the first element of array, array[0], which is itself an array. That pointer has the same value as a pointer to array[0][0], but different type. In fact, your code is incorrect in that the argument's type and the function parameter's type do not agree, but as long as the types int(*)[] and int * have the same representation, the reinterpretation will work in practice.

And as for the print function, in this case, am I correct to say that by printing ar[0], I am printing array[0][0]?

Yes, when you call print() as you do in your example, provided that type representations agree as described above.

But why is it that for the case of printing *(ar+1), I am actually printing array[0][1] instead of array[1][0]?

Because ar is a pointer to the first element of array, i.e. the 1D array array[0]. Its elements are array[0][0], array[0][1], etc.. ar + 1 is therefore a pointer to the second element of array[0], which is array[0][1].

You have two options to make your code correct, both of which might help clarify the issues involved. You could change the argument you pass to print() to agree with the function:

print(array[0]);

Alternatively, you could change the declared type of print()'s argument, whith a corresponding alteration to its use:

void print(int (*ar)[2]) {
    printf("%d\n", ar[0][0]);
    printf("%d\n", *(ar+1)[0]);
}

or

void print(int ar[][2]) {
    printf("%d\n", ar[0][0]);
    printf("%d\n", *(ar+1)[0]);
}

The latter two change the meaning of *(ar + 1).

Upvotes: 1

Mic1780
Mic1780

Reputation: 1794

When you pass an array to a function, you are using "pass-by-reference" meaning you pass the address of the array.

  • Am I passing address of the first 1-D array (array[0]) or am i just passing the address of the very first variable (array[0][0]).

No. The way you are passing array currently you are giving the function the 2-D array.

  • And as for the print function, in this case, am I correct to say that by printing ar[0], I am printing array[0][0]?

No. You still need to specify the number you want to print from ar[0]

  • But why is it that for the case of printing *(ar+1), I am actually printing array[0][1] instead of array[1][0]?

This is because you are telling the program to go to array in memory and use pointer arithmetic to traverse the array. For example, if you have a char * str variable that contains characters abcde\0 and want to get the character at position 2 you can either do str[2] or *(str+2) which means the same thing and both give the character 'c'.

Upvotes: 0

Sunil Bojanapally
Sunil Bojanapally

Reputation: 12658

If you are passing a two-dimensional array to a function:

int array[NROWS][NCOLUMNS];
print(array);

the function's declaration must match:

void print(int array[][NCOLUMNS])
{ ... }

or

void print(int (*ap)[NCOLUMNS]) /* ap is a pointer to an array */
{ ... }

The 2D arrays which is array of arrays decays a pointer to an array rather than a pointer to a pointer.

Upvotes: 2

hlscalon
hlscalon

Reputation: 7552

Your code is probably not doing what you want. Compile with Wall and you will see some warnings.

You need to change print argument to accept a 2D array (now you are passing int (*)[2] but the function is expecting int *), and then you can print the values correctly.

#include <stdio.h>
void print (int ar[][2]);

int main () {
    int array[2][2] = {
        {1,2},
        {3,4}
    };
    print(array);
    return 0;
}

void print (int ar[][2]) {
    printf("%d\n", ar[0][0]); // 1
    printf("%d\n", ar[1][0]); // 4
}

Upvotes: 0

Related Questions