``````int *p;
int *q;
p=new int;
*p=35
q=new int;
*q=62;
p=q;
cout<< *p << ","<< *q<< endl;
``````

What is the value of `*p` and `*q`? Why?
the statement p=q refer to?

Edited by mike_2000_17: Fixed formatting

4
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by BevoX
``````int *p;
int *q;
p=new int;
*p=35
q=new int;
*q=62;
p=q;
cout<< *p << ","<< *q<< endl;``````

wat is the value of *p n *q?why?
the statement p=q refer to?

I don't see a variable named n, so I assume n means "and" here.

You need to put [/code] (added above) after the code to close the code tags and you need to rewrite your questions in English please.

``````int *p;
int *q;
p=new int; // allocate memory for first int
*p=35;      // assign value of 35 to the allocated int
q=new int; // allocate memory for second int
*q=62;      // assign value of 62 to the allocated int
p=q;         // let p point to the same memory location as q
// resulting in a memory leak for first int
cout<< *p << ","<< *q<< endl; // display "62,62"``````

When you are dealing with pointers for the first time, it might be confusing, when do you need to put a star in front of a variable, and actually what it means.

int* p - is a pointer of type 'int'. It can hold an address of type 'int'. So its type is virtually int* . When you declare a pointer, you declare as type*, for example:

``````char* ptr1;
double* ptr2;``````

Why am I saying this? Because some may like to declare pointers like one of these.

``````int *ptr
int * ptr; //or this
int     *            ptr; //or whatever``````

They are the same thing, but it might be a little confuseing, especially when you start to use the derefernce operator. So consider a pointer as a type and a star after it. They stick together.

We've got our pointer and, we can put a memory address in it.

``````int* ptr;
int x = 5;

ptr = &x; // assign the memory address of 'x' to 'ptr'``````

'&' - is the reference operator - it gives back the address of a variable. Now 'ptr' is holding the address of 'x'. But this is a memory address, and if I print it out, I would get some strange value. But here comes the * operator. By saying *ptr means to the computer, give the user back the actual value underneath, that memory address. Which is the variable 'x', which is 5.

``````cout << *ptr; //this is the value -> 5
cout << ptr; //this is the address, what 'ptr' holding, which is the address of variable 'int x'``````

I can use the * operator to give 'x' a new value, because 'ptr' holds the address of 'x', and by saying *ptr = 8; I can manipulate the actual value of 'x'. // I am dereferencing here.

``````int x = 5;
int y = 0;
int *ptr;

ptr = &x;
*ptr = 8; // 'x' is now holding 8, -- dereferencing 'ptr'

*ptr = &y; /* This would make no sense, because this means to the computer,
I want to put the MEMORY ADDRESS of variable 'y' into 'x', where we store an actual int value.*/

*ptr = *&y; //This would be OK.  Referencing the same variable, and then dereferencing it a bit pointless, but it would be OK. !!!Important ptr has been previously initialized with the address of 'x'! Without that, the program would crash, beacuse 'ptr' just would hang in the air, pointing somewhere at the memory. And we would try to give to that 'somewhere' area, a value.``````

Now take a look at your code.

``````int *p; // declare a pointer of type int
int *q; // declare a pointer of type int

p = new int; //assign memory size of an int
*p = 35; // put the value '35' in that memory area

q = new int; //assign memory size of an int
*q = 62; // put the value '62' in that memory area

p = q; /* make 'p' equal to 'q' . 'p' is now holding what 'q' is holding, 'q' is holding the memory address of '62'. And by doing that you just lost track of '35' now you have no reference to that memory area. It is still there, but you don't know where it is anymore, because you've lost its address. You've replaced it with the address of '62'. This is a memory leak.*/

cout<< *p << ","<< *q<< endl; /* you are dereferencing 'p' which gives back the value of 62, q - same*/``````

At last, there is difference between pointer initialization, and dereferencing.

``````int z = 3;
int* ptr = &z; // declaration and initialization of 'ptr'
*ptr  = 5; //dereferencing 'ptr' and giving it a new value - actually giving 'z' a new value.

int* ptr_2; // declaration of ptr_2
*ptr_2 = a; // dereferencing an uninitialized pointer is wrong, it just hangs in the "air".``````

I hope this helped a little bit. There maybe some errors, please forgive me, my native language is not english.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.