473,801 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf("%d",*(& ptr2 + 2))

Dear all,

why in the following program

#include<stdio. h>
#include<stdlib .h>

int main(void)
{

int x[] = {99,2,3,4,5};
int *ptr,**ptr2;

ptr = x;
ptr2 = &ptr;

printf("%d",*(& ptr2 + 2));

return EXIT_SUCCESS;
}

printf("%d",*(& ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
Jun 27 '08 #1
27 2128
sophia wrote:
Dear all,

why in the following program

#include<stdio. h>
#include<stdlib .h>

int main(void)
{

int x[] = {99,2,3,4,5};
int *ptr,**ptr2;

ptr = x;
ptr2 = &ptr;

printf("%d",*(& ptr2 + 2));

return EXIT_SUCCESS;
}

printf("%d",*(& ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
Where do people find these contrived examples?

The answer is luck. I just happens that's the way the variables are
laid out in memory.

Try building 64 bit and see what you get then.

--
Ian Collins.
Jun 27 '08 #2
On Apr 25, 12:56*pm, Ian Collins <ian-n...@hotmail.co mwrote:
sophia wrote:
Dear all,
why in the following program
*#include<stdio .h>
*#include<stdli b.h>
*int main(void)
*{
* *int x[] = {99,2,3,4,5};
* *int *ptr,**ptr2;
* *ptr *= x;
* *ptr2 = &ptr;
* *printf("%d",*( &ptr2 + 2));
* *return EXIT_SUCCESS;
*}
printf("%d",*(& ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?

Where do people find these contrived examples?

The answer is luck. *I just happens that's the way the variables are laid out in memory.
There is something more than luck, that is my conclusion.

I tried changing the value of x[0] to different values but still
it will print the o/p correctly.

Has it any thing to do with the compiler ?

I checked this program on lcc-win32 compiler
Try building 64 bit and see what you get then.
I don't have a 64 bit compiler
Jun 27 '08 #3
sophia wrote:
Dear all,

why in the following program

#include<stdio. h>
#include<stdlib .h>

int main(void)
{

int x[] = {99,2,3,4,5};
int *ptr,**ptr2;

ptr = x;
ptr2 = &ptr;

printf("%d",*(& ptr2 + 2));

return EXIT_SUCCESS;
}

printf("%d",*(& ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
What it prints is purely by chance.
Did your compiler not produce a warning?
What is the type of &ptr2? It's int ***. What is the type of (&ptr2 +
2). Again the same. Then what's the type of *(&ptr2 + 2)? It's int **.
What type does %d expect. It expects an int.

What you want is:

printf("%d\n", **ptr2);

Jun 27 '08 #4
sophia wrote:
On Apr 25, 12:56*pm, Ian Collins <ian-n...@hotmail.co mwrote:
>sophia wrote:
Dear all,
why in the following program
#include<stdio. h>
#include<stdlib .h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr *= x;
ptr2 = &ptr;
printf("%d",*(& ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2)); is printing the first element of the
array x ? can anybody explain ?

Where do people find these contrived examples?

The answer is luck. *I just happens that's the way the variables are
laid out in memory.

There is something more than luck, that is my conclusion.
Your conclusion is wrong. What it prints is purely by chance, since
there is a type discrepancy between what printf expects and what it
gets.
I tried changing the value of x[0] to different values but still
it will print the o/p correctly.
That's irrelevant.
Has it any thing to do with the compiler ?
Certainly your output, because your program invokes undefined behaviour,
is likely to be specific to your implementation or even specific to
each run of the program.
I checked this program on lcc-win32 compiler
>Try building 64 bit and see what you get then.
I don't have a 64 bit compiler
Jun 27 '08 #5
sophia wrote:
On Apr 25, 12:56 pm, Ian Collins <ian-n...@hotmail.co mwrote:
>sophia wrote:
>>Dear all,
why in the following program
#include<stdio. h>
#include<stdlib .h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr = x;
ptr2 = &ptr;
printf("%d",*(& ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",* (&ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?
Where do people find these contrived examples?

The answer is luck. I just happens that's the way the variables are laid out in memory.

There is something more than luck, that is my conclusion.

I tried changing the value of x[0] to different values but still
it will print the o/p correctly.
The luck is in the memory layout, not the values. The address of &ptr2
+ 2 just happens to be the address of x[0].
Has it any thing to do with the compiler ?
Everything, memory layout is compiler and platform specific, although
most 32 bit compilers on x86 probably use the same layout.
I don't have a 64 bit compiler
Every home should have one :)

--
Ian Collins.
Jun 27 '08 #6
sophia wrote:
<snip>
) * *int x[] = {99,2,3,4,5};
) * *int *ptr,**ptr2;
)>
) * *ptr *= x;
) * *ptr2 = &ptr;
)>
) * *printf("%d",*( &ptr2 + 2));
<snip>
)>
) printf("%d",*(& ptr2 + 2)); is printing the first element of the array
) x ? can anybody explain ?
<snip>
)
) you see, i am not playing games by printing numbers. this is a
) question which was asked in an interview

Then it's not a very good interview, unless they're looking for very
specific in-depth knowledge on how a specific compiler works.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Jun 27 '08 #7
sophia said:
On Apr 25, 2:31 pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>sophia said:


Dear all,
why in the following program
#include<stdio. h>
#include<stdlib .h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr = x;
ptr2 = &ptr;
printf("%d",*(& ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?

The program is broken. In any case, it's overly complex for its job.
<snip>
you see, i am not playing games by printing numbers. this is a
question which was asked in an interview
Fine, then the answer is easy. "The program is broken, because the
expression (&ptr2 + 2) forms an illegal pointer value." That will get you
full marks, provided that the interviewer knows C. If the interviewer only
/thinks/ he knows C, either become a mind-reader very quickly or look
elsewhere for a job.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jun 27 '08 #8
sophia wrote:
On Apr 25, 2:31*pm, Richard Heathfield <r...@see.sig.i nvalidwrote:
>sophia said:


Dear all,
why in the following program
#include<stdio. h>
#include<stdlib .h>
int main(void)
{
int x[] = {99,2,3,4,5};
int *ptr,**ptr2;
ptr *= x;
ptr2 = &ptr;
printf("%d",*(& ptr2 + 2));
return EXIT_SUCCESS;
}
printf("%d",*(& ptr2 + 2)); is printing the first element of the
array x ? can anybody explain ?

The program is broken. In any case, it's overly complex for its job.

First, decide what you want to print. Then try to think of the
simplest reasonable way to print it. If you get stuck with that, let
us know.

For example, it would appear from the above that you wish to print
the number 3. The simplest way to do this is as follows:

#include <stdio.h>

int main(void)
{
puts("3");
return 0;

}

you see, i am not playing games by printing numbers. this is a
question which was asked in an interview
Unless the interviewer provided specific additional details regarding
the memory layout of the implementation, the question is broken and the
program is undefined according to the language standard. You might try
seeking a job elsewhere. Even if it were to be assumed that the
array 'x' starts 2 * sizeof &ptr2 bytes after &ptr2, the program is
still broken because the printf call is incorrect.

Jun 27 '08 #9
Richard Heathfield wrote:
sophia said:
>Dear all,

why in the following program

#include<stdio. h>
#include<stdlib .h>

int main(void)
{

int x[] = {99,2,3,4,5};
int *ptr,**ptr2;

ptr = x;
ptr2 = &ptr;

printf("%d",*(& ptr2 + 2));

return EXIT_SUCCESS;
}

printf("%d",*( &ptr2 + 2)); is printing the first element of the array
x ? can anybody explain ?

The program is broken. In any case, it's overly complex for its job.

First, decide what you want to print. Then try to think of the
simplest reasonable way to print it. If you get stuck with that, let
us know.

For example, it would appear from the above that you wish to print the
number 3. The simplest way to do this is as follows:

#include <stdio.h>

int main(void)
{
puts("3");
return 0;
}
Even simpler and correct would be:

/* begin code */
#include <stdio.h>

int main(void)
{
putc('3', whatever_stream );
return 0;
}
/* end code */

:-)

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.