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;
}
3
  • 1
    Please inline that code here, it's not very long at all. Commented Sep 7, 2011 at 7:23
  • 5
    To answer your question, frankly, it isn't needed at all. The author of the code apparently doesn't like double-indexing, was demonstrating the different ways to work with pointers, or other reason. Commented Sep 7, 2011 at 7:26
  • i just want know how this stuff works.. Commented Sep 10, 2011 at 5:26

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

"... make p also an array (i.e., also a pointer to an area in memory)" No, arrays are not pointers.
Corrected and added a note at the end as well.
"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 ints and nothing else. There's no pointer.
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

Comments

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");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.