sorry for the previous attachments, ive now posted the code below.
I understand that there is an appropriate formatter for pointers and
by using %p you get a number which is the address of what the pointer
is referencing.
But I’m curious about the following behaviour thats occuring.
where the code reads:
buffer[100] = 765;
…
printf(“%f \n”, ptr);
how does ptr manage to print 765?
if it was to print a value surely
it would be a random one as its set to point to the first
element in buffer?
With my understanding so far I was initially expecting ptr to print
a random long value because I’m not using the dereference syntax “*”
which is used to get any value from it?
What seems to happen when I use *ptr with prinf and %f (occuring as
the last instruction at the end of the code) is that a long random
value prints to the console (opposite to what i expected)
Whys this behaviour happening?
Code:
int main()
{
double* ptr;
double buffer[1024];
buffer[0] = 435;
/* ptr prints 765 why? */
printf("%f \n", ptr);
/*set ptr to the first elements address of buffer */
ptr = buffer;
buffer[100] = 765;
/* ptr prints 765 why? */
printf("%f \n", ptr);
/* this prints the address that ptr points to */
printf("%p \n", ptr);
/* why is void used here? */
printf("%p \n", (void*)ptr);
/* ppoint ptr to the addres of the last element in buffer */
ptr = &buffer[1024];
buffer[1003] = 500;
printf("%f \n", ptr);
printf("%f \n", *ptr);
}