int* ptr=10;.......issue.

A

al pacino

hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.

int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.
//
.......
}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.
 
L

Lars Uffmann

al pacino said:
whats the issue with the following declaration, is it correct and what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).
on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

You need to add a typecast:
int* ptr = (int *) 10;
or
int *ptr = new int[1];
*ptr = 10;
delete [] ptr;

Best Regards,

Lars
 
A

al pacino

Lars said:
al pacino said:
whats the issue with the following declaration, is it correct and what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).
maybe i was confused as,
char *s="hello";
implies 's' pointing to the string HELLO.

but in the original case..what was happning was
int *ptr; and
ptr=10; //ptr now points to address 10;

why is it so in the case of int as in char.

thanks and regards.



on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

You need to add a typecast:
int* ptr = (int *) 10;
or
int *ptr = new int[1];
*ptr = 10;
delete [] ptr;

Best Regards,

Lars
or
 
S

Salt_Peter

al said:
hi ,

whats the issue with the following declaration, is it correct and what
exactly is happening here.

int main()
{
//.....
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

no, thats wrong and a big no-no.
You don't understand pointers.
//
......
}


on gcc it gives no error...but when i compile it using g++ it gives
"invalid conversion of int * to int"

please advise on this.
regards.

That is expected. You are inducing undefined behaviour.
You are not trying to *set* what is *at* the pointer to the value of
10.
You are trying to set the memory address *in* ptr to 10.
A very big no-no (thankfully caught by most C++ compilers).

This too is illegal:

int *ptr; // ptr holds some residual value, its unitialized (bad,
no-no, danger)
*ptr = 10;

How do you know what is at that residual location?
Imagine what happens when the compiler lets you overwrite any memory
location.
Answer: UB (format the hard drive, crash the computer, etc)
You think thats a common joke - but its not a joke

Pointers are always set using the address_of (&) operator or new.

int n; // reserve integer n on the stack, although n is uninitialized
int *ptr = &n; // reserve pointer ptr on stack & its now valid: it
holds n's address
*ptr = 10; // same as n = 10

Again, ptr holds an address, *ptr is the variable at that address.
And the term in question is "dereferencing the pointer".
One way or another, its the program that sets the pointer.
Pointers are not a toy, when you see a pointer, except for
polymorphism, a buzzer should go off in your head. Pointers are nearly
always bad news.

pointers are hell
pointers create bugs
pointers generate headaches
a pointer is a disease
etc

Stay away from pointers, use a reference instead.
hmm, how the hell are we going to explain references to you?
 
G

Gernot Frisch

pointers are hell
pointers create bugs
pointers generate headaches
a pointer is a disease
etc

Stay away from pointers, use a reference instead.

Do you come from Java? Don't make pointers that bad, because they do
have their right of existance.


hmm, how the hell are we going to explain references to you?

It's apointer - but not really a pointer.
 
R

Robert J. Hansen

Do you come from Java? Don't make pointers that bad, because they do
have their right of existance.

Yes, when wrapped in a tr1::shared_ptr<>, or some other suitable
exceptionsafe, leak-free method of handling them.
 
R

Rolf Magnus

al said:
Lars said:
al pacino said:
whats the issue with the following declaration, is it correct and what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location which
stores the value 10.

Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).
maybe i was confused as,
char *s="hello";
implies 's' pointing to the string HELLO.

Well, there is a difference here. That string literal is of array type. An
array gets in most cases converted to a pointer to its first element.
but in the original case..what was happning was
int *ptr; and
ptr=10; //ptr now points to address 10;

why is it so in the case of int as in char.

It's not char, it's char[6].
 
R

Rolf Magnus

Gernot said:
Do you come from Java? Don't make pointers that bad, because they do
have their right of existance.

I'd say that Java references are closer to C++ pointers than to C++
references.
 
P

peter koch

Gernot Frisch wrote:
Saltiers(?) said:
Do you come from Java? Don't make pointers that bad, because they do
have their right of existance.
Is it not rather the other way around - that Java people abuse pointers
because that is the only thing they have? We "real" C++ people normally
do know that Saltiers advice (avoid pointers whenever possible) is
sound and correct.
It's apointer - but not really a pointer.

2% truth 98% false!

/Peter
 
S

Salt_Peter

Gernot said:
Do you come from Java? Don't make pointers that bad, because they do
have their right of existance.

I do enjoy Java. Did you know that a Java reference is the equivalent
of a C++ pointer? Java would love to have the equivalent of a C++
reference.
It's apointer - but not really a pointer.

No, a reference is an object (even when it is implemented as a pointer
down under -which is often not the case). A plain pointer is nothing
more than an adrress to something or nothing that may or may not be
valid.
 
L

Lars Uffmann

Salt_Peter said:
pointers are hell
pointers create bugs
pointers generate headaches
a pointer is a disease
etc
Stay away from pointers, use a reference instead.

Lol - now come on - pointers are pretty useful and impossible
to work without in many cases.

Best Regards,

Lars
 
R

Rolf Magnus

Salt_Peter said:
I do enjoy Java. Did you know that a Java reference is the equivalent
of a C++ pointer? Java would love to have the equivalent of a C++
reference.


No, a reference is an object

No. That's exactly what a reference is not.
(even when it is implemented as a pointer down under -which is often not
the case).

Really? Could you elaborate? Which compilers implement them differently? I'm
seriously interested in that.
 
B

Bo Persson

al pacino said:
Lars said:
al pacino said:
whats the issue with the following declaration, is it correct and
what
exactly is happening here.
[...]
int* ptr=10; // i suppose ptr is pointing to the memory location
which
stores the value 10.

Nope, this would make the pointer point to the memory ADDRESS 10
(0xa).
maybe i was confused as,
char *s="hello";
implies 's' pointing to the string HELLO.

but in the original case..what was happning was
int *ptr; and
ptr=10; //ptr now points to address 10;

why is it so in the case of int as in char.

thanks and regards.

Sorry, but you try to draw the conclusions the wrong way.

Here int follows the general rules, and char* is very special. It has
a whole set of extra rules, to be able to cope with the strange string
literal "hello" (which isn't a char!).


Bo Persson
 
P

peter koch

Lars Uffmann skrev:
Lol - now come on - pointers are pretty useful and impossible
to work without in many cases.
Correct. But that does not invalidate Salt_Peter's point. And I've seen
pointless cases where for one bad reason or the other pointers have
been used instead of a normal variable. Even in the cases where normal
variables can't be used raw pointers are almost never correct (unless
you garbage collect, of course).

/Peter
 
F

Frederick Gotham

Salt_Peter posted:
pointers are hell
pointers create bugs
pointers generate headaches
a pointer is a disease
etc


When posting to comp.lang.c++, it's wise to assume that your audience is
competant. Competant programmers can use pointers perfectly well.
 
S

Salt_Peter

Frederick said:
Salt_Peter posted:



When posting to comp.lang.c++, it's wise to assume that your audience is
competant. Competant programmers can use pointers perfectly well.

lol

If you do understand the limitations and intricacies of pointers, they
can certainly be very useful in the hands of a competant programmer.
I'm not one of those "competant" programmers.
As far as the audience here is concerned, i've learned way too much in
this newsgroup to hold its members in disrespect.
 
U

Unknownmat

al said:
maybe i was confused as,
char *s="hello";
implies 's' pointing to the string HELLO.

but in the original case..what was happning was
int *ptr; and
ptr=10; //ptr now points to address 10;

why is it so in the case of int as in char.

This is a reasonable question. I think you'll find that C++ has a
number of quirks and inconsistencies.

In this case, I think that C++ is handling the pointers consistently,
but that a string literal such as "hello" is actually a character array
- in this case char[ 6 ]. And when you set anything equal to an array
you get the starting address of the array.

This means, among other things, that if you derefernced "s" (in your
example) such as: char A = *s; A would take the value of the character
'h'.

Does that make sense?
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top