What will the compiler react if I initialize a pointer like "Int*pt=6"?

V

vib.cpp

What will the compiler react if I initialize a pointer in this way?
Int *pt=6;
I know it is not right,but i do not why?can anybody make it more clear?
3ks in advance,
 
J

jason.cipriani

What will the compiler react if I initialize a pointer in this way?
Int *pt=6;
I know it is not right,but i do not why?can anybody make it more clear?
3ks in advance,

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
 
V

vib.cpp

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:

$B!J(Ba$B!K(B
main$B!J!K(B
{
int *pa;
pa=6;
}

or

$B!J(Bb$B!K(B
main$B!J!K(B
{
int *pa;
*pa=6;
}

and

main$B!J!K(B
{
int *pa;
*pa=6;
}
is not right so.


but why it works if 6 is replaced by 0:

main()
{
int *pa=0;
}
 
J

jason.cipriani

thank you joson:
when i write:


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
 
J

jason.cipriani

thank you joson:
when i write:


I guess some of these things aren't answered there, so:

main()
{
int *pa=6;}

it will be equivalent as:

$B!J(Ba$B!K(B
main$B!J!K(B
{
int *pa;
pa=6;

}

or

$B!J(Bb$B!K(B
main$B!J!K(B
{
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$B!J!K(B
{
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
 
J

James Kanze

when i write:

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;
}

(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.
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.)
 
V

vib.cpp

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- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -- Hide quoted text -

- Show quoted text -



if assign a '0' or a '6' (both as an int)with a different response,is
it also some kind of "overload"?
 
J

jason.cipriani

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
 
R

Rolf Magnus

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.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top