Check the following code. Its related to the pointer to an array concept

107 views Asked by At

Consider the following code

#include<stdio.h>
void main()
{
    int s[4][2] = 
    {  
        {20,1}, 
        {21,2}, 
        {22,3}, 
        {23,5}  
    };    
    int (*p)[2];
    int i,j,*pint;

    for( i=0;i<=3;i++)
    {
        p=&s[i];
        pint= (int *)p;

            for(j=0;j<=1;j++)
            { 
                printf("%d \n", *(pint + j));
            }
    }
}

The code basically creates a 2-D array, then creates a pointer to 1-D array. Then the pointer to the 1-D array is initialized with the code p = &s[i];

The next line initializes a pointer to an integer 'pint'. pint = (int *)p;

I was wondering why the following line of code doesn't work. It makes logical sense. pint = p;

4

There are 4 answers

0
haccks On

It will not compile.

 for(j=0;j<=;j++)
            ^Missing the limit   

It should be

 for(j=0;j < 2;j++)  

See the working code here: http://ideone.com/CvNlxG

3
Dr.Tower On

p is of type int (*)[2]. pint is of type int*.

They are two different types, which is why the statement pint = p; will not work.

3
Sergey L. On

p is type int (*)[2] which is related to s and indeed any assignment

p = s + (int)x;

will work. But pint is of type int *. Although you are casting p to that type you are pushing it for undefined behaviour.

What you probably want is

int i,j,*pint;

for( i=0;i<=3;i++)
{
    pint = s[i]; // pint points to the two ints in row i

        for(j=0;j<=1;j++) // i assume you missed the one here
        { 
            printf("%d \n", *(pint + j));
        }
}
0
jthill On

In C the value of an array is a pointer to its first element. You have:

    int s[4][2];
    int (*p)[2];
    int i,j,*pint;

So let's look at the types in your expressions:

        p=&s[i]; /* & here is unsafe, it's a trap, see at end. */

Left hand side, pointer to array of two ints, right hand side, exactly the same.

        pint= (int *)p;

Right hand side is forcibly converted, no need to examine further.

                printf("%d \n", *(pint + j));

pint is pointer to int, so pint+j points j ints past what pint points at, * that is an int.

which all work, and you want

    pint = p;

which doesn't. Left hand side is pointer to int, right hand side is pointer to array of two ints, see above.

So you can do

    pint = *p;

or

    pint = p[0];

which has exactly the same semantics -- in fact, p[j] is defined to be *(p+j), which works, because of the array value semantics.

Or you could use the

    pint = &p[0];

construction I flagged above as a trap. It's the same as the previous expression, here, because of the rule, but it breaks an extremely commonly-used equivalence:

int *P = p[0]; 

works for all of the following declarations of p:

int p[1][1];
int (*p)[1];
int **p;

but &p[0] doesn't, and the breakage is more likely than you might expect, C programmers simply expect that code built to work with fixed-size arrays also works with variable-sized ones just by changing declarations.