1

This program prints all arrays outputs correctly. But how does this program work exactly? Why do we need address of s[i] here?

#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}
Matt
  • 22,721
  • 17
  • 71
  • 112
White Dwarf
  • 189
  • 1
  • 3
  • 11

3 Answers3

2

int (*p)[2] is a pointer to an int [2] type, which itself is essentially a int * type but with some additional type checking on the array bounds.

So in p = &s[i]; you are setting p to be the address of the pointer to the area in memory where the array s[i] is.

It's much much easier to just make p also an array (i.e., "essentially" a pointer to an area in memory with additional machinery), and then use that directly to point at the array memory area (p = s[i]). However, in that case, that's exactly what pint (as a true pointer instead) is doing, so we can remove p entirely.

So:

#include <stdio.h>

int main(){
        int s[4][1] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int i,j,*pint;

        for(i=0;i<4;++i){
                pint = (int*)s[i];
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

See Arrays and pointers or google for "C arrays and pointers" and also Pointer address in a C multidimensional array.

Note, I assume you are just doing this to play around and understand how pointers and arrays interact, so I make no comment on whether it is ideal to use pointers arithmetic or array notion and so on in each case.


Note also, that I say an array is "essentially a pointer [...]", however, this is not strictly true, it just acts like a pointer in a lot of cases and for the most part this is a reasonable way to think of how things are working. In reality, arrays are treated in a special way. See Is array name equivalent to pointer? and Arrays.

Community
  • 1
  • 1
MGwynne
  • 3,512
  • 1
  • 23
  • 35
  • "... make `p` also an array (i.e., also a pointer to an area in memory)" No, arrays are **not** pointers. – Keith Thompson Sep 07 '11 at 07:54
  • Corrected and added a note at the end as well. – MGwynne Sep 07 '11 at 08:16
  • "So in `p = &s[i];` you are setting `p` to be the address of the pointer...". This is false. There is no pointer to take the address of in this context. This code makes `p` to point to an `int[2]` array. That arrays consists of two `int`s and nothing else. There's no pointer. – AnT stands with Russia Jun 28 '13 at 18:44
1
#include <stdio.h>

int main(){
        int s[4][2] = {{1234,1},{1233,2},{1232,3},{1331,4}};
        int (*p)[2];
        int i,j,*pint;

        for(i=0;i<4;++i){
                p = &s[i];
                pint = (int*)p;
                printf("\n");
                for(j=0;j<=1;++j)
                        printf("%d ",*(pint+j));
        }
        return 0;
}

here p is defined as an array which can store two integer values

 int (*p)[2];

in the outer for loop we use p array to store the 4 {1 dimensional} . 1 at a time .

 p = &s[i];

that is in first iteration it will be

p = &s[0] ;
i.e p = {1234,1}

the next iteration will be

 p = &s[1] ;
i.e p = {1234,2}

and so on. that is why we need &S[i]

the inner loop just iterate throug the 1 dimensional array to print the element

Sameer Surjikar
  • 511
  • 6
  • 12
1

Ugh. I'm not sure what this code is supposed to illustrate other than how to make easy-to-understand code hard to understand.

s is a 4-element array of 2-element arrays of int. That is, for i in 0..3, the type of s[i] is int [2] (which in most contexts decays to int *), and the type of &s[i] is int (*)[2].

p is a pointer to a 2-element array of int. Each time through the loop, p is assigned the address of the 2-element array at s[i].

Finally, pint is a simple pointer to int, and each time through the loop is set to point to the first element in the array pointed to by p. *(pint + j) is the long way of writing pint[j].

Note that both p and pint are entirely superfluous; this is basically a long-winded way of writing

for (i = 0; i < 4; i++)
{
  for (j = 0; j < 2; j++)
    printf("%d ", s[i][j]);
  printf("\n");
}
John Bode
  • 119,563
  • 19
  • 122
  • 198