Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is `*(multi + row)` producing a pointer address instead of a value?

Why is *(multi + row) producing a pointer address instead of a value? Im confused but there must be a good explanation, but i dont know still.

#include <stdio.h>
#define ROWS 5
#define COLS 10
int multi[ROWS][COLS];
int main(void)
{
    int row, col;
    for (row = 0; row < ROWS; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            multi[row][col] = row*col;
        }

    }
    for (row = 0; row < ROWS; row++)
    {
        for (col = 0; col < COLS; col++)
        {
            printf("\n%d ",multi[row][col]);
            printf("%d ",*(*(multi + row) + col));
        }
    }
    return 0;
}
like image 758
kandelvijaya Avatar asked Jan 21 '26 18:01

kandelvijaya


1 Answers

It doesn't. multi is a two-dimensional array, so dereferencing the pointer which it decays into when you perform pointer arithmetic on it results in an array (which is COLS elements wide).

Steps:

  1. int multi[ROWS][COLS]; - here multi is a two-dimensional array.

  2. multi + row - here multi decayed into a pointer of type int (*)[COLS]

  3. *(multi + row) - this is equivalent with multi[row], i. e. the type of this expression is int[COLS].