Click here to Skip to main content
15,037,420 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
main( )
{
int s[5][2] = {
{ 1234, 56 },
{ 1212, 33 },
{ 1434, 80 },
{ 1312, 78 }
} ;
int ( *p )[2] ;
int i, j, *pint ;
for ( i = 0 ; i <= 3 ; i++ )
{
p = &s[i] ;
pint = p ;
printf ( "\n" ) ;
for ( j = 0 ; j <= 1 ; j++ )
printf ( "%d ", *( pint + j ) ) ;
}
}


What I have tried:

I have tried to print 'p' instead of 'pint' because the value of 'pint' and 'p' are equal but it printed the different values .
Posted
Updated 9-Feb-21 3:22am

I suggest you to have a look at the following webpage: cdecl: C gibberish ↔ English[^].

in order to print 'p', you could use
C
for ( j = 0 ; j <= 1 ; j++ )
{
  printf ( "%d ", (*p)[j]);
}
   
First off, indent your code. That makes it a whole load more readable, even in simple examples like this!
C++
main( )
    {
    int s[5][2] = { { 1234, 56 },
                    { 1212, 33 },
                    { 1434, 80 },
                    { 1312, 78 } } ;
    int ( *p )[2] ;
    int i, j, *pint ;
    for ( i = 0 ; i <= 3 ; i++ )
        {
        p = &s[i] ;
        pint = p ;
        printf ( "\n" ) ;
        for ( j = 0 ; j <= 1 ; j++ )
           printf ( "%d ", *( pint + j ) ) ;
        }
    }
Second, particularly when you are beginning, always use curly brackets, even if they are not needed:
C++
for ( j = 0 ; j <= 1 ; j++ )
   {
   printf ( "%d ", *( pint + j ) ) ;
   }
That way it's harder for you to make a mistake, and assume that code you add is part of the loop.

Thirdly, when you compile that app, you will get a warning: "assignment from incompatible pointer type" because p and pint are not the same type:
pint is a "pointer to an integer", p is an "array of pointer to integers" - or a "pointer to a pointer to an integer"
And you get different values as a result when you try to print them, if only because *pint is an integer and thus 32 bits long, and *p is pointer and thus 64 bits long.

Don't ignore warnings: at this stage the compiler knows a lot better than you do! (And I do, sometimes - I have my compiler options set to treat warnings as errors so I can;t ignore them. If I get a warning it almost always means I made a mistake, not the compiler!
   
v2
Comments
Richard MacCutchan 9-Feb-21 9:33am
   
Are you confusing p and pint?
OriginalGriff 9-Feb-21 9:52am
   
Yep. :O
Oops - fixed.
CPallini 9-Feb-21 9:52am
   
p is not an array of pointers to integer.
That would be if p was declared as
int *p[2];

but, as it stands,
int (*p)[2];

it is a 'pointer to array of integer' (that was 'surprisingly' for me too).
satyendra1090 9-Feb-21 10:45am
   
I didn't get .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900