Re: printf("%d",*(&ptr2 + 2))
- From: Sri Harsha Dandibhotla <harsha.dsh@xxxxxxxxx>
- Date: Fri, 25 Apr 2008 12:16:56 -0700 (PDT)
On Apr 25, 1:07 pm, santosh <santosh....@xxxxxxxxx> wrote:
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.
I have a doubt over here(doubt, not question, because I doubt what I
think is true). I think your reasoning is wrong here. &ptr2 is value
of address type. &ptr + 2 is also an address location. *(address
location) gives the value stored at that location which is being
interpreted as int because of the %d. It is a different matter that
the memory layout had the array preceded by the pointer declarations
but apart from that, the print statement is valid. Please correct me
if I am wrong.
What you want is:
printf("%d\n", **ptr2);
What part of Sophia's code led you to believe that was what she wanted?
.
- Follow-Ups:
- Re: printf("%d",*(&ptr2 + 2))
- From: lawrence . jones
- Re: printf("%d",*(&ptr2 + 2))
- From: santosh
- Re: printf("%d",*(&ptr2 + 2))
- References:
- printf("%d",*(&ptr2 + 2))
- From: sophia
- Re: printf("%d",*(&ptr2 + 2))
- From: santosh
- printf("%d",*(&ptr2 + 2))
- Prev by Date: Re: Garbage collection
- Next by Date: Re: printf("%d",*(&ptr2 + 2))
- Previous by thread: Re: printf("%d",*(&ptr2 + 2))
- Next by thread: Re: printf("%d",*(&ptr2 + 2))
- Index(es):
Relevant Pages
|