C++ pointer concepts - c++

so, I've got my pointer:
int *p = new int(10);
And I print out the following things:
&p which is: 0xbdee018
p which is: 0xb8c254b0
&p stands for the address of the pointer, and p is stands for the address of the mapped value by the pointer itself.
Is that correct, or is it the very opposite?

Yes exactly. & gives the address of the element.
So
p gives the value currently inside the variable - p being a pointer, you get a pointer (to an int)
&p returns the address of p
Graphically
p value = 0xb8c254b0 --> int x 10
^
|-------- 0xbdee018

Yes you are right .
&p stand for address of pointer and p give address where it point.

p denotes the address of an integer, e.g. int*.
*p denotes the integer stored at p (dereferencing), e.g. int.
&p denotes the address of the pointer p. If p would be an int, you would obtain a pointer-to-int by referencing p, e.g. int*.

Yes you're totally correct.
&p is the the address of the pointer or you can say a pointer of your pointer.
p is the pointer of an array of 10 int elements.

I think you are getting thing in the correct way. A pointer has to be stored somewhere, so it has an address too. Try to see the memory with the debugger, by using the address you shown in your example, point to the address
0xbdee018
and check the bytes, you should see, in some orders the bytes:
0xb8c254b0
ie p.
The order of the bytes depends on the 'endianity' of the cpu you are using

Yes, it seems, that you got it right.
To ilustrate:
int i = 42;
int * pointer_to_i = &i;
Then:
i == 42 // obvious
&i == pointer_to_i // address, where a value 42 is stored
// for the sake of example, let's assume
// that &i == 0x12345678
&pointer_to_i // address, where a value 0x12345678 is stored
Additionally:
&<name> gets the address of a variable <name> (you can assign that to a pointer);
*<pointer> dereferences the <pointer>, that is retreives the actual data stored in a place pointed to by that pointer.

P is a pointer to int.(*p) means value at the pointer p which will be value of that int where p is pointing.(&p) will give you address of the pointer,means *(&p)=p.

I think you need to dereference the pointer to get at the value.
Try printing *p, as this prints the value stored at the address, this is a dereferenced pointer.
When you print &p, you are printing the address of the address of p.
Printing p prints the pointer to p.

Pointer is a variable that stores address of other variable that the pointer points to. This variable itself also has memory address, which is &p in this case if variable name is p. So:
"&p" is stands for the address of the pointer, and "p" is stands for the address of the mapped value by the pointer itself.
You are right. Besides, *p will get you the value of the object it points to. You may find Binky Pointer Fun Video useful.

In C++, many people prefer int* p over int *p. There is absolutely no difference between the two from a language point of view, it's just a matter of human perception.
int* p says: "p is a pointer to an integer".
&p is the memory location at which p is stored (ragardless of the data type of p).
p is the pointer itself. Its value is a memory location at which an integer ist stored (except if p == 0).
*p is (a reference to) the value that p points to.
In C, many people prefer int *p over int* p. It says: "*p is an integer". This has the advantage that you do not fall into traps like int* p, q, which declares an int* p and an int q.

For pointers (e.g., int* p):
p is the pointer (i.e., address of the pointed object)
&p is the address of the pointer.
*p is the pointed object (dereferencing).
For non pointers (e.g., int p):
p is the data
&p is its address

Related

Why Do I get the memory adress instead of the real value ? Pointer c++

int x = 2;
int y=8;
int* p = &x;
*p=y;
cout << p <<endl;
my question is: why do I get the memory adress when I Print p and not the actual value since I already dereferenced it in line 4
cout << *p << endl;
is what you need.
When you try to output something to stdout, compiler automatically infer its type and call the corresponding function. In your case, p is pointer type, therefore the address is printed, and since *p is int type, you should use *p if you want to print the value.
I think you have a misconception about dereferencing pointers.
...since I already dereferenced it in line 4
Dereferencing means to retrieve the value the pointer is pointing to. It doesn't change the pointer itself in any way. p is still a pointer after *p=y;.
All *p=y does, is to change the value p is pointing to, it doesn't change the pointer itself.

What is the difference in both pointer?

int main(int argc, char *argv[])
{
int *p=5,**P;
printf("%d",&P);
}
What is the difference between *p and **P?
*p is a pointer which points to an int that is equal to 5
**P is a pointer to A pointer;it is a variable that contains an address.
A pointer is a variable that contains an address. In your PC every variable is stored in a certain place in its memory. The exact place where a variable is placed is called variable's address.
With a pointer you are able to know the exact address of another variable.
Example
int c = 5; // this value of this int is stored at a certain address;
int *p = &c; // the pointer p now contains the address where 5
Keep in mind that *p is a variable too and as such is stored somewhere in the memory as well.
int **P = &p ; // a double pointer that contains the address of the pointer p
this will be a new pointer that points to the address where p is stored ( not the variable c!) –a pointer;
A single asterisks represents a pointer whereas a double asterisks represents a pointer to a pointer.
*p is an address to an int that equals 5. So somewhere in memory, there is an int that equals 5 and that pointer points to that address of that int.
**p is an address to a pointer in memory. So think of it that we have a pointer pointing to the int above, but now on top of that, we have another pointer pointing to the pointer for the int. Another way to think of it is we have an address for another address that's for the int.

little confusion regarding C++ pointers and this powerpoint slide

In the above powerpoint slide, why is it that:
int* p = &x;
cout << *p << endl;
outputs 25? From the little understanding that I have with pointers, &x is the address of x, which is assigned to *p, the value of p. In that case, since &x is 0003, so shouldn't *p and its output also be 0003?
For all the advanced coders out there who cannot stand a simpleton like myself, I apologize in 'advance'.
&x is the address of x, which is assigned to *p
No, there is nothing assigned to *p.
In that line p is defined with type int* and p is assigned the address of x.
Now p holds the address 0003 and *p holds 25.
The * only relates to the variable definition in that line. During initialization you assign values to the variable you define.
If that wasn't the case, where should p point to in first place? Into which location should the 0003 be written then?
The slide is confusing, since it suggests that the name of the pointer variable were *p. The name of the variable is just p.
When you print the expression p, you will get 0003, since that is the value of the pointer.
So the pointer points to the object at address 0003, and the expression *p gets you the value from there, which is 25.
The slide should better say that cell 0001 has the name p. And that cell 0003 has two names (or paths to get there), which are x directly or *p indirectly.
When we declare pointer we use * with datatype for e.g int *p it doesn't mean that we are declaring as well as referring to value at the same time.
So when you type
int *p = &x;
it is similar as writing
int *p;
p = &x;
Now p is 0003 and value at address 0003 is 25. which you can get by *p.
The reference operator(&) refers to the address of variable x,which is assigned to p.Since p is holding the address of a memory location it is of pointer type (int*).Now the (*p) ,where (*) is the deference operator(*) refers to the value that the address in p is holding.

initialising a pointer without giving an address

I want to know the statement below is not valid,
int* p;
*p = 3;
but this statement below is
int* p; int a;
a = 9;
p = &a;
*p = 3;
Why cannot I give *p a value before giving it an address but can give it after assigning an address. Thanks
A pointer is just a special type of variable that holds a memory address as its value. Before being initialized, it could possibly point to any random memory address.
Dereferencing a pointer (using the *p = 3 syntax) is telling the computer to go to the memory address pointed to by p, and store the value 3 in that location.
So it should be obvious that without a valid memory location, this is problematic. Here's one possible way of obtaining a valid memory address via allocation:
int *p = new int;
*p = 3;
The first line does two things: 1) allocates memory on the heap for an int, and 2) sets the value of pointer p to the address of the allocated memory.
Without being initialized to point to an address, a pointer will contain some trash address, which is what you attempt to write to in the first example. To be valid, it is essential that the pointer references some valid memory that you own, which requires that you set it to an address.

What value does a Pointer to a Pointer print if not a single dereference operator is used at all?

I have tried the output of the following code. But, i don't yet understand what might the value of q represent.
I understand that the *q points to p implying that, printing *q would print the address of p, where as **q would print the value at the address p points to, i.e., x(=5).
#include <iostream>
using namespace std;
int main()
{
int x=5;
int *p,**q;
p=&x;
q=&p;
cout<<q;
return 0;
}
So what does q alone represent? What is that value that is printed when just q is printed?
Just print value and ardess of your variable and you will see:
x: 5 &x: 0x7fff691dfcc4
p: 0x7fff691dfcc4 &p: 0x7fff691dfcb8
q: 0x7fff691dfcb8 &q: 0x7fff691dfcb0
&var - location;
var - value
You say:
printing *q would print the address of p
However this is not true. Printing q would print the address of p. After all, you assigned that in doing q = &p;.
Printing *q would print the value of p , which is the address of x.
Since q is a pointer to p, the value of q is the location of p, whatever that means on this particular platform. (Most likely the memory address containing p's value.)
(I will assume 64-bit pointers and 32-bit integers in this answer, just to have concrete values to write about. It holds with any vaues, though).
Let's analyse p first. p is 8 bytes storing an address (of the variable x). Printing *p will print the value residing at that address, which is the 4 bytes of the value of variable x. Printing just p will print the 8 bytes of the address stored in p.
Now, the same applies to q. q is 8 bytes storing an address of the variable p. So printing *p will print the value residing at that address, which is the 8 bytes of the value of p. Printing just q will print the 8 bytes of the address stored in q.
int x = 1;
A block of memory is allocated to hold int value. And that block is named as 'x'. Let's say x is allocated at 0x1234.
int *p;
Here 'p' is pointer to int that means p would contain the address of some int. Let's say p is allocated address 0x2345.
p = &x;
This would make p contain the address of x i.e 0x1234 would be stored in the location allotted to p.
int **q;
Here q is pointer to pointer to an int that means q would contain the address of pointer to int. Let's say q is allotted address 0x3456.
q = &p;
This would make q contain the address of p i.e 0x2345 would be stored in the location allotted to q.
Hope I am simple and clear...
Pointer is just a data type, being not very different from other types.
It stores an address of another data object, and you can use *p to access the data object.
So, pointers are not different from other types but their contents have some special meaning.
(int**) is such a pointer that points to a data object of type (int*), being not very different from other pointers (they may point to data object of type int).
So, the content of q is a pointer to p. *q indicates you use the content to find p.
the content of p is a pointer to an Integer, *p indicates you use the content to find the integer.
By adding ** before q in declaration of q, you are saying to compiler that, q is a pointer which will point to address of a pointer. So when you are printing q, you are actually printing address of p pointer, not the value of address p point to (i.e. x).
In your code, p is a pointer which is storing an address of an integer. But p itself has an address, which you are storing in q.