-1
#include <stdio.h>

void main (void){
    int mat [5][5],i,j;
    int *p;            
    p = &mat [0][0];
    for (i=0;i<5;i++)
        for (j=0;j<5;j++)
            mat[i][j] = i+j;
    printf ("%d\t", sizeof(mat));   i=4;j=5;
    printf( "%d", *(p+i+j));
}

Can somebody help me with the output of this snippet . I get it the sizeof(mat) will print 50 . But help me with second printfc

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621

2 Answers2

2

Try changing this second statement to:

printf("%d",*(p)+i+j );

I'm guessing you mean to print the sum of the value pointed at by p and the values stored in i and j

Norman Gray
  • 11,978
  • 2
  • 33
  • 56
kwierman
  • 441
  • 4
  • 11
2

*(p+i+j) will print the value stored at the address p + i*sizeof(int) + j*sizeof(j)

As array are stored in a linear way in the stack and i equals 4 and j equals 5 this will show the value of the 9th int of the array hence mat[1][4] which is equal to 5

Also for the result of sizeof(mat), it will be 5*5*sizeof(int). I assume here your int are stored on two bytes because you said it would print 50 but it totally depends on your computer. It is more usually 4 bytes long on todays computer so it could also print 100.

Valentin Mercier
  • 5,256
  • 3
  • 26
  • 50