How did it react when you tried it? Also, there is no such type as
"Int".
The reason it is not right is because 'pt' is a pointer to an int, and
6 is just an int. The types are not compatible. You can not initialize
a pointer's value from an integer.
Jason
thank you joson:
when i write:
main()
{
int *pa=6;
}
it will be equivalent as:
(a)
main()
{
int *pa;
pa=6;
}
or
(b)
main()
{
int *pa;
*pa=6;
}
and
main()
{
int *pa;
*pa=6;
}
is not right so.
but why it works if 6 is replaced by 0:
main()
{
int *pa=0;
}
I strictly refuse to answer any of these questions until after you've
at least watched the video I linked to in your other thread:
http://cslibrary.stanford.edu/104/
Once you watch that, come back with any additional questions you might
have. It covers pretty much all of what you did below.
Jason
I guess some of these things aren't answered there, so:
> main()
> {
> int *pa=6;}
>
> it will be equivalent as:
>
> (a)
> main()
> {
> int *pa;
> pa=6;
>
> }
>
> or
>
> (b)
> main()
> {
> int *pa;
> *pa=6;
>
> }
It ("int *pa=6") is equivalent to (a). Neither (a) nor (b) are
correct, however, and (a) should not compile. With (a), you are
attempting to set the pointer itself to the value 6 -- this is
something like trying to make it point to memory address 6. It doesn't
make any sense, and is not valid. With (b), you should now be able to
spot the problem.
> and
>
> main()
> {
> int *pa;
> *pa=6;}
>
> is not right so.
>
> but why it works if 6 is replaced by 0:
>
> main()
> {
> int *pa=0;
>
> }
It works with 0 because 0 is a special case value representing a NULL
pointer (although it is generally clearer if you use "NULL" instead of
"0", #include <cstring> to get NULL). In general, you can't assign an
integer to a pointer, but the standard makes an explicit exception for
the value 0.
Jason
> > How did it react when you tried it? Also, there is no such
> > type as "Int".
> > The reason it is not right is because 'pt' is a pointer to
> > an int, and 6 is just an int. The types are not compatible.
> > You can not initialize a pointer's value from an integer.
> when i write:
> main()
Won't compile. You need a return type for main.
> {
> int *pa=6;
Won't compile. There's no implicit conversion of int to
pointer.
> }
> it will be equivalent as:
> (a)
> main()
> {
> int *pa;
> pa=6;
> }
> or
> (b)
> main()
> {
> int *pa;
> *pa=6;
> }
None of the above. Both of the above involve assignment; the
original didn't. But (a) won't compile, because there's no
implicit conversion of int to pointer. You can't initialize a
pointer with an int, and you can't assign an int to a pointer.
And (b) is undefined behavior, since you dereference an
uninitialized pointer.
> and
> main()
> {
> int *pa;
> *pa=6;
> }
> is not right so.
> but why it works if 6 is replaced by 0:
> main()
> {
> int *pa=0;
> }
Because C++ is broken:-).
Seriously, there is a special rule concerning "integral constant
expressions evaluating to zero"---they're called "null pointer
constants", and although they do have type int, they convert
implicitly to any pointer type, giving a null pointer. (And
this is confusing, since a "null pointer constant" isn't a
pointer, but a "null pointer" is.)
--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
if assign a '0' or a '6' (both as an int)with a different response,is
it also some kind of "overload"?
No, the term "overload" is not applicable here. It's not related to
what you are talking about.
"Overloading" generally applies to functions and to operators. When
you define a function that has the same name as another function, but
has a different parameter list, that's called overloading a function:
void printvalue (double d);
void printvalue (int n);
void printvalue (const char *str);
There, printvalue() is overloaded.
When you define an operator for a specific type, that's called
overloading an operator:
class MyClass {
public:
// an overloaded == operator:
bool operator == (const MyClass &) const;
};
// an overloaded << operator:
ostream & operator << (ostream &, const MyClass &);
Jason
> It ("int *pa=6") is equivalent to (a). Neither (a) nor (b) are
> correct, however, and (a) should not compile. With (a), you are
> attempting to set the pointer itself to the value 6 -- this is
> something like trying to make it point to memory address 6. It doesn't
> make any sense, and is not valid.
Well, it can make sense. And if you add a cast, you can make it valid, too.
But if you need that, you know that you do, and you know why.