What is the difference in both pointer? - c++

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.

Related

How can C++ array pointers have different location, yet point to the same element?

I know the variable that holds the array stores the address of the array and any indexing on it performs a pointer arithmetic on the same. While I was brushing up my basics this is what I found.
int main() {
int equal;
char c[] = {'a', 'b', 'c'};
char* p = c;
char* q = &c[0];
cout<<*p<<endl;
cout<<*q<<endl;
cout<<&p<<endl;
cout<<&q<<endl;
if (c==q) {
equal=1;
}
cout<<equal<<endl;
}
//output
a
a
0x7ffda22e0a60
0x7ffda22e0a68
1
Derefencing p,q is giving me the same value, yet the addresses of p,q are different. How is this possible?
When you allocate an array, that variable you assign the array to points to the beginning of that array. The beginning of that array is an address in the memory.
The address of the beginning of an array is the address of its first element, so it doesn't matter whether you take char *x = arr; or char *y = &arr[0]; they will be the same.
Now x and y are allocated on the stack and are two different variables, so they will oviously have different addresses. Hence
&x == &y // false
When writing T* x you are declaring a pointer to an object of type T. This pointer x is essentially an integral value containing a number that represents an address in the memory.
A pictorial representation of your code: (addresses are imaginary)
p and q are separate variables, they have their own address. However, the address they point to, are the same.

What exactly is the purpose of the (asterisk) in pointers?

I'm new to programming and I'm trying to wrap my head around the idea of 'pointers'.
int main()
{
int x = 5;
int *pointerToInteger = & x;
cout<<pointerToInteger;
}
Why is it that when I cout << pointerToInteger; the output is a hexdecimal value, BUT when I use cout << *pointerToInteger; the output is 5 ( x=5).
* has different meaning depending on the context.
Declaration of a pointer
int* ap; // It defines ap to be a pointer to an int.
void foo(int* p); // Declares function foo.
// foo expects a pointer to an int as an argument.
Dereference a pointer in an expression.
int i = 0;
int* ap = &i; // ap points to i
*ap = 10; // Indirectly sets the value of i to 10
A multiplication operator.
int i = 10*20; // Needs no explanation.
One way to look at it, is that the variable in your source/code, say
int a=0;
Makes the 'int a' refer to a value in memory, 0. If we make a new variable, this time a (potentially smaller) "int pointer", int *, and have it point to the &a (address of a)
int*p_a=&a; //(`p_a` meaning pointer to `a` see Hungarian notation)
Hungarian notation wiki
we get p_a that points to what the value &a is. Your talking about what is at the address of a now tho, and the *p_a is a pointer to whatever is at the &a (address of a).
This has uses when you want to modify a value in memory, without creating a duplicate container.
p_a itself has a footprint in memory however (potentially smaller than a itself) and when you cout<<p_a<<endl; you will write whatever the pointer address is, not whats there. *p_a however will be &a.
p_a is normally smaller than a itself, since its just a pointer to memory and not the value itself. Does that make sense? A vector of pointers will be easier to manage than a vector of values, but they will do the same thing in many regards.
If you declare a variable of some type, then you can also declare another variable pointing to it.
For example:
int a;
int* b = &a;
So in essence, for each basic type, we also have a corresponding pointer type.
For example: short and short*.
There are two ways to "look at" variable b (that's what probably confuses most beginners):
You can consider b as a variable of type int*.
You can consider *b as a variable of type int.
Hence, some people would declare int* b, whereas others would declare int *b.
But the fact of the matter is that these two declarations are identical (the spaces are meaningless).
You can use either b as a pointer to an integer value, or *b as the actual pointed integer value.
You can get (read) the pointed value: int c = *b.
And you can set (write) the pointed value: *b = 5.
A pointer can point to any memory address, and not only to the address of some variable that you have previously declared. However, you must be careful when using pointers in order to get or set the value located at the pointed memory address.
For example:
int* a = (int*)0x8000000;
Here, we have variable a pointing to memory address 0x8000000.
If this memory address is not mapped within the memory space of your program, then any read or write operation using *a will most likely cause your program to crash, due to a memory access violation.
You can safely change the value of a, but you should be very careful changing the value of *a.
yes the asterisk * have different meanings while declaring a pointer variable and while accessing data through pointer variable. for e.g
int input = 7;
int *i_ptr = &input;/*Here * indicates that i_ptr is a pointer variable
Also address is assigned to i_ptr, not to *iptr*/
cout<<*i_ptr;/* now this * is fetch the data from assigned address */
cout<<i_ptr;/*it prints address */
for e.g if you declare like int *ptr = 7; its wrong(not an error) as pointers ptr expects valid address but you provided constant(7). upto declaration it's okay but when you go for dereferencing it like *ptr it gives problem because it doesn't know what is that data/value at 7 location. So Its always advisable to assign pointers variable with valid addresses. for e.g
int input = 7;
int *i_ptr = &input;
cout<<*i_ptr;
for example
char *ptr = "Hello"; => here * is just to inform the compiler that ptr is a pointer variable not normal one &
Hello is a char array i.e valid address, so this syntax is okay. Now you can do
if(*ptr == 'H') {
/*....*/
}
else {
/*.... */
}

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.

C++ pointer concepts

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