its giving the different address !!!!!!!!!!

S

sushant

hi,

the code

int main(void)
{
int x=10,*p;
p=&x;
printf("%p",p);
return 0;
}
is showing the o/p as FFBE but if i give printf("%p",&x); is giving the
o/p as FFBC... i am not able to make out the reason... is it bcos of
the stack implementation ???? its just a wild guess..
 
R

Ravi Uday

sushant said:
hi,

the code

#include said:
int main(void)
{
int x=10,*p;
printf ("%p\n", (void *)&x);
/* %p requires a void*, so cast it. */
p=&x;
printf("%p",p);

return 0;
}
is showing the o/p as FFBE but if i give printf("%p",&x); is giving the
o/p as FFBC... i am not able to make out the reason... is it bcos of
the stack implementation ???? its just a wild guess..

After you assign p = &x, both points to same address.
 
R

Ravi Uday

sushant said:
but they are not they are given diff values as i have already mentioned

Do you think you can make out anything in your reply/question above ??
Give some context before and after to whom and what you are replying to !!

On my system it works fine :)

bash-2.02$ cat t1.c
#include <stdio.h>

int main(void)
{
int x=10,*p;
p=&x;
printf("p = %p\n",(void *)p);
printf ("&x = %p\n", (void *)&x);
return 0;
}

bash-2.02$
bash-2.02$ gcc -pedantic -ansi -Wall temp1.c
bash-2.02$
bash-2.02$
bash-2.02$ ./a.exe
p = 0x244ff40
&x = 0x244ff40
bash-2.02$


HTH
- Ravi
 
J

Jonathan Burd

sushant said:
hi,

the code

Are we your relatives?
int main(void)
{
int x=10,*p;
p=&x;
printf("%p",p);
return 0;
}
is showing the o/p as FFBE but if i give printf("%p",&x); is giving the
o/p as FFBC... i am not able to make out the reason... is it bcos of
the stack implementation ???? its just a wild guess..

1. Can you read this?

#include <stdio.h>
#define hmm int c,
#define crap char**
int main(hmm crap v){int
i=1876723,x=i,*p;i=98297;p=&x;"fxaaskdjh";{"laksjdl";
}for(i=99;i/*somemorecrap*/<150;++
i){"foobazbar";}return 0;}

I hope you get the idea behind indentation
and clean code.

2. From the standard: 7.19.6.1

p The argument shall be a pointer to void. The value
of the pointer is converted to a sequence of
printing characters, in an implementation-defined
manner.

What stack implementation? What wild guess? Why keep
insisting on using unintelligible short forms? Just
use complete sentences, can you not?

Regards,
Jonathan.
 
S

sushant

just give only one printf statement in your code like printf("%p",
(void *)p); and then replace this statement by printf("%p", (void
*)&x); and check out whether you are getting the same o/p or not
.....ive checked this code in borland tc++ 2.01, i was geting diff o/p
as ive already mentioned in the original question .....
 
S

sushant

just give only one printf statement in your code like printf("%p",
(void *)p); and then replace this statement by printf("%p", (void
*)&x); and check out whether you are getting the same o/p or not
.....ive checked this code in borland tc++ 2.01, i was geting diff o/p
as ive already mentioned in the original question .....
 
A

Alex Fraser

sushant said:
just give only one printf statement in your code like printf("%p",
(void *)p); and then replace this statement by printf("%p", (void
*)&x); and check out whether you are getting the same o/p or not
....ive checked this code in borland tc++ 2.01, i was geting diff o/p
as ive already mentioned in the original question .....

So you're running a different (version of the) program, and getting
different output. And this shocks you so much you feel the need for ten
exclamation marks because...?

Even if you ran the exact same program twice, there's nothing to say the
output must be the same.

What is probably happening in this case is that when you display the address
of x, the compiler notices that you do not do anything useful with p, so no
space is allocated for it, causing the address of x to change.

You may find that declaring x and p in the opposite order means you get the
same result whether you print the address of x or the value of p.

Alex
 
S

sushant

thnx a tonne alex but can you explain me why on reversing the order of
declaration of x and *p it is giving the same o/p when we replace that
statement with another one ?
 
A

Alex Fraser

sushant said:
thnx a tonne alex but can you explain me why on reversing the order of
declaration of x and *p it is giving the same o/p when we replace that
statement with another one ?

It's just a possible side-effect of how the compiler works; not something
you should rely on, or (apart from curiousity) care about.

If the compiler uses a descending-address stack to store automatic variables
(which it almost certainly does) and it decides to use the highest address
for x (the decision is often influenced by the order of declaration), that
address is likely to be the same regardless of whether p is optimised away
by the compiler or not.

Alex
 
M

Mike Wahler

sushant said:
just give only one printf statement in your code like printf("%p",
(void *)p); and then replace this statement by printf("%p", (void
*)&x); and check out whether you are getting the same o/p or not

So you compile one form, run it, and see the output;
then modify, recompile and run again, and get different
output? I would not be at all surprised by that, in
fact I'd *expect* different output. You're running
two separate programs, which can (and almost always will)
be loaded at different addresses.
....ive checked this code in borland tc++ 2.01, i was geting diff o/p
as ive already mentioned in the original question .....

Your testing method is extremely flawed.

BTW what exactly are you trying to determine?

-Mike
 
M

Mike Wahler

sushant said:
thnx a tonne alex but can you explain me why on reversing the order of
declaration of x and *p it is giving the same o/p when we replace that
statement with another one ?

Please read again and carefully consider what Alex wrote.
There's absolutely no reason to expect the address values
output to be anywhere near one another. They're *two
separate programs*. And e.g. a 'virtual memory' system
might (and often does) move things around so that subsequent
invocations of the exact same program could give different outputs.
The only consistent outputs you should expect are of those specific
values you assign to your objects. In the case of addresses, that's
why we must use 'pointer = &object' syntax instead of
'pointer == some_literal_value'. '&object' means 'address of object',
*wherever that may happen to be*. The only time you can expect
(actually you're guaranteed) that a pointer will retain its value is
during the current execution of the scope where its value was initialized
or assigned. (But note the exception that if the pointer was given its value
from 'malloc()' or 'realloc()', a call to 'free()' with that same value as
its argument releases the language from that guarantee.

BTW please stop top-posting to comp.lang.c.
Thank you.


-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top