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



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?
.



Relevant Pages

  • Re: Parameter Name Warning?
    ... the names on the declarations do not matter, it should perhaps give a warning regarding different names being used. ... > there is more to those names than just what the compiler does with them. ... > You know only what you get in the header. ... >>> int main ...
    (microsoft.public.vc.language)
  • Re: is order urgent doubt
    ... Which *compiler* are you using? ... layout of the warning messages.) ... i = sizeof(long int); ... about the code in the editor vs. the code in the source file. ...
    (comp.lang.c)
  • Re: Whats the deal with size_t?
    ... ES> allowed to emit diagnostics that C doesn't require. ... in warning about a signed to unsigned conversion. ... a size_t to an int it is quite justified in warning about the reverse, ... The compiler is also quite justified in warning if you use a signed ...
    (comp.lang.c)
  • Re: Compendiums of compiler warning/error messages mapped to actual code problems?
    ... The general problem is of course that the compiler messages must be short, and that tends to make them so cryptic that it isn't always immediately obvious what the problem is. ... int this; ... Warning twarn.c: ...
    (comp.lang.c)
  • Re: matrix stuff (solving b = A*x) --> using numerical recipes
    ... but then I removed it since I couldn't see any compiler warnings/errors without stdio.h). ... turn the warning level up to the maximum ... void banmul(float **a, unsigned long n, int left, int right, float x, float b); ...
    (comp.lang.c)