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;
}
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:
int multi[ROWS][COLS]; - here multi is a two-dimensional array.
multi + row - here multi decayed into a pointer of type int (*)[COLS]
*(multi + row) - this is equivalent with multi[row], i. e. the type of this expression is int[COLS].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With