0

I've just come across this program that deals with multidimensional arrays. While I've got the gyst of it, there's a particular notation that I'm not sure of.

The idea is to pass a pointer to the column of a multidimensional array, because 2-Dim arrays do not require you to mention the number of rows in the 2D matrix. So, a pointer pm to the column of the 2D matrix is passed as: int (*pm)[COLUMN]

Here's the full program:

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

const int ROW  = 2;
const int COL=3;

void fill_array(int (*pm)[COL], int row);
void display_array(int m[][COL], int row);

int main(int argc, char const *argv[])
{
    /* code */

    int i,j;
    int m[ROW][COL];

    //CALL function to enter elements
    fill_array(m,ROW);

    //display
    display_array(m,ROW);
    return 0;
}

void fill_array(int (*pm)[COL], int row)
{
    int i,j;

    printf("Please fill the array's content: \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<COL;j++)
        {
            printf("\n m[%d][%d]: ",i,j);
            scanf("%d", &pm[i][j]);
        }
    }
}

void display_array(int m[][COL], int row)
{
    int i,j;

    printf("\n Array's contents: \n");
    for(i=0;i<row;i++)
    {
        for(j=0;j<COL;j++)
        {
            printf("%d\t", m[i][j]);
        }

        printf("\n");
    }
}

I'm not sure about the representation of a pointer to an array. What I mean is I'm not familiar with this notation where a pointer is just appended to the array. Could someone please throw some light on this?

Many thanks!

Manish Giri
  • 3,562
  • 8
  • 45
  • 81
  • `void fill_array(int (*pm)[COL], int row);`is the same as `void fill_array(int pm[][COL], int row);`. – Jabberwocky Nov 14 '14 at 10:21
  • http://stackoverflow.com/questions/1052818/create-a-pointer-to-two-dimensional-array http://stackoverflow.com/questions/3904224/declaring-a-pointer-to-multidimensional-array-and-allocating-the-array – Antonio Nov 14 '14 at 10:29

4 Answers4

2

When an array is passed by value to a function then it is converted to a pointer to its first element. According to the C Standard (6.3.2.1 Lvalues, arrays, and function designators)

3 Except when it is the operand of the sizeof operator or the unary & operator, or is a string literal used to initialize an array, an expression that has type ‘‘array of type’’ is converted to an expression with type ‘‘pointer to type’’ that points to the initial element of the array object and is not an lvalue. If the array object has register storage class, the behavior is undefined.

If you pass this array

int m[ROW][COL];

to a function that it is converted to pointer to its first element that is in turn an array of type int[COL]. A pointer to this array will look like

int ( * )[COL]

If you will dereference this pointer you will get one-dimensional array of type int[COL]

SO for example if you declared a pointer to a one dimensional array like

int ( *pm )[COL]

then expression

*pm

has type of int[COL]

Expression *pm is equivalent to pm[0] As pm[0] is an array then you may apply the subscript operator a second time pm[0][0] and will get the first element of type int of the two-dimensional arra.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

As an argument of a function, int m[][COL] is equivalent to int (*m)[COL], in the same way int v[] is equivalent to int *v. In both cases, m is treated as a pointer to an integer array of size COL.

The important bit is that *m (dereferencing m) gives you the first array (just like *v gives you the first element in that vector). And pointer arithmetic on m works as expected, e.g. *(m+1) gives you the second array. As a consequence, you can index m as if it was a two-D array (m[i][j]) inside the function.

downhillFromHere
  • 1,967
  • 11
  • 11
-1

I am still relatively new to C but I believe it is a pointer to an array of pointers. The only way I know how to make a dynamic 2 dimensional array is like that.

-1

Well, if I can say the real truth is that in C/C++ there is no array more than allocated consecutive space in the memory. There is a pointer that points to the beginning of the allocated memory block. in that case if you have array of [10] int elements, the allocated space is about sizeof(int)*10 and the pointer points to the first element. Because of that you could easily increments ++ and decrements -- to move around.

With dimensional arrays it is easy to pass the pointer(s) to their first elements and increment around to columns. However, you could easily work with array[2][3] with a single pointer. You just need to multiply 2 * 3 to capture the whole allocated memory block.

celeborn
  • 308
  • 2
  • 13
  • You are wrong, C/C++ have arrays more over even multidiemnsional arrays. – Vlad from Moscow Nov 14 '14 at 10:34
  • Well, we are talking for the standard arrays. They are actually allocated space shifted each time. because of that pointers and arrays are so related to each other. – celeborn Nov 14 '14 at 10:36
  • By the way, you can check Jerry Chain course of programming paradigms http://see.stanford.edu/errors/default.aspx?aspxerrorpath=/see/courseinfo.aspx – celeborn Nov 14 '14 at 10:45
  • It is no matter how arrays are realized in one or other language. Nevertheles an array is not a pointer in C/C++. – Vlad from Moscow Nov 14 '14 at 10:54
  • Take into account that it is not me who downvoted your answer. But in any case your answer is wrong. – Vlad from Moscow Nov 14 '14 at 10:56
  • Yes, I do not mean that it is a pointer. I meant that pointer is actually the beginning of the array. The whole idea was to explain why the pointers and arrays are so strongly related in this two languages. Cheers! – celeborn Nov 14 '14 at 10:58