*p=7

N

niklaus

int *p=7;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

Is there any difference in this case from C . Does C++ differ from C in
this case.

the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

Can someone tell me why it s implementation defined and where it
differs ,
can't i say value of p after int *p=0 or int *p=3 is 0 or 3
 
H

Howard

int *p=7;

That sets the pointer p to point to address location 7, which may or may not
be a valid location, depending on your operating system and compiler. It
does NOT set *p to the value 7. And even if it did, you'd need to allocate
space for *p in the first place, either using new or by assigning the
address of an existing int object to it. (See end of post.)
int main()
{
int *p=0;

That sets p to be a NULL pointer. The value 0 in a pointer means that it
doesn't currently point at anything. It is illegal to dereference such a
pointer.
printf("%x\n",p);

Here, you're printing the address contained in p, which is 0. If you tried
printing *p, you'd have a problem!
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

Is there any difference in this case from C . Does C++ differ from C in
this case.

the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

Can someone tell me why it s implementation defined and where it
differs ,
can't i say value of p after int *p=0 or int *p=3 is 0 or 3

I imagine it's implementation defined because assigning 7 or 0 to a pointer
_might_ be valid on some system. (Certainly, it's not valid on Windows or
MacOS.)

If you want to assign the value 7 to an int* variable, then you could, for
example, do this:

int* p = new int(7); // allocates one int, and initializes it to the value
7

or

int* p = new int; // allocate one int
*p = 7; // assign 7 to *p

or

int i = 7;
int*p = &i; // set p to point to the address of i (which holds the value 7)

-Howard
 
D

Daniel T.

int *p=7;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

Is there any difference in this case from C . Does C++ differ from C in
this case.

the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

Can someone tell me why it s implementation defined and where it
differs ,
can't i say value of p after int *p=0 or int *p=3 is 0 or 3

int *p = 0; is not implementation defined. It is defined to be a "null
pointer"

int *p = 3; is implementation defined. There is no telling what is at
memory location 3, or even if there *is* a memory location 3.
 
S

Salt_Peter

int *p=7;

sets a global pointer ::p to 7, assuming that thats a valid address.
int main()
{
int *p=0;

sets a local pointer p to null
printf("%x\n",p);
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

You simply declared a distinct local pointer and set it to 0. Is there
any reason why it should not print 0?
Is there any difference in this case from C . Does C++ differ from C in
this case.

the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

Can someone tell me why it s implementation defined and where it
differs ,
can't i say value of p after int *p=0 or int *p=3 is 0 or 3

Your question is not clear. You never set the values *at* p or *at* ::p
above.
While setting a null pointer's target to any value is undefined
behaviour, printing the value of the pointer itself is not. Also,
setting the global *:):p) to any value depends on whether ::p is a
valid address in this case. So the latter is implementation defined.
Your code, however, is not testing these aspects.

Study the following:

#include <iostream>

int n(10); // globals
int *p = &n;

int main()
{
int n(-1); // locals
int *p = &n;

std::cout << "::p = " << ::p;
std::cout << "\n";

std::cout << "p = " << p;
std::cout << "\n";

std::cout << "::n = " << ::n;
std::cout << "\n";

std::cout << "n = " << n;
std::cout << "\n";

return 0;
}

/* the pointers on your platform will vary, but thats irrelevent here
::p = 0x500d90
p = 0x7fff93ee95ac
::n = 10
n = -1
*/

There is no ambiguity whatsoever between ::p and p as well as n and
::n.
Perhaps at this point you might consider rephrasing the question.
 
K

Kai-Uwe Bux

int *p=7;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

Is there any difference in this case from C . Does C++ differ from C in
this case.

In C++ your very first line "int* p = 7;" is a compile time error. Maybe in
C, it is not -- who knows.
the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

That seems to deal with C and is off-topic here.
Can someone tell me why it s implementation defined and where it
differs , can't i say value of p after int *p=0 or int *p=3 is 0 or 3

After "int* p = 0;" p is guaranteed to be the null-pointer (for C++, that
is). The second assignment does not make sense in C++ and should not
compile.


Best

Kai-Uwe Bux
 
O

Old Wolf

Salt_Peter said:
You simply declared a distinct local pointer and set it to 0. Is there
any reason why it should not print 0?

Yes ! The printf statement causes undefined behaviour, because
%x is for printing unsigned ints, but you gave it a pointer.

What's likely to happen is that you get the bit-pattern of the
pointer re-interpreted as an unsigned integer. But you might
also get demons flying out your nose.
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top