By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,649 Members | 696 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,649 IT Pros & Developers. It's quick & easy.

*p=7

P: n/a
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

Sep 18 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

<ni*****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
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
Sep 18 '06 #2

P: n/a
ni*****@gmail.com wrote:
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.

--
There are two things that simply cannot be doubted. Logic and our
ability to sense the world around us. Doubt those, and you no longer
have anyone to discuss it with, nor any ability to discuss it.
Sep 18 '06 #3

P: n/a

ni*****@gmail.com wrote:
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.

Sep 18 '06 #4

P: n/a
ni*****@gmail.com wrote:
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

Sep 18 '06 #5

P: n/a
Salt_Peter wrote:
ni*****@gmail.com wrote:
int *p=0;
printf("%x\n",p);
prints 0 on all the platforms i test or i set a breakpoint at printf

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.

Sep 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.