Not able to understand the notations : * and ** with pointers - c++

I have a problem with the pointers. I know what this does:
*name
I understand that this is a pointer.
I've been searching but I do neither understand what this one does nor I've found helpful information
**name
The context is int **name, not multiplication
Could someone help me?

NOTE: Without the proper context, the usage of *name and **name is ambiguous. it may portrait (a). dereference operator (b) multiplication operator
Considering you're talking about a scenario like
char * name;
char **name;
in the code,
*name
name is a pointer to a char.
**name
name is a pointer, to the pointer to a char.
Please don't get confused with "double-pointer", which is sometimes used to denote pointer to a pointer but actually supposed to mean a pointer to a double data type variable.
A visual below
As above, we can say
char value = `c`;
char *p2 = &value; // &value is 8000, so p2 == 8000, &p2 == 5000
char **p1 = &p2; // &p2 == 5000, p1 == 5000
So, p1 here, is a pointer-to-pointer. Hope this make things clear now.

It's actually very simple, consider this:
int a; // a is an int
int* b; // b is a pointer to an int
int** c; // c is a pointer to a pointer to an int
If you see every level as just another variable type (so, see *int as a type), it's easier to understand.
Another example:
typedef int* IntPointer;
IntPointer a; // a is an IntPointer
IntPointer* b; // b is a pointer to an IntPointer!
Hope that helps!

pointer stores address of variable, pointer to pointer stores address of another pointer.
int var
int *ptr;
int **ptr2;
ptr = &var;
ptr2 = &ptr;
cout << "var : " << var;
cout << "*ptr : " << *ptr;
cout << "**ptr2 : " << **ptr2;
You can look here

int a = 5;// a is int, a = 5.
int *p1 = &a; // p1 is pointer, p1 point to physical address of a;
int **p2 = &p1; // p2 is pointer of pointer, p2 point to physical adress of p1;
cout<< "a = "<<a << " *p1 = "<<*p1<<" *(*p2) = " << *(*p2)<<endl;

**name in this case. Would be a pointer to a pointer.

Related

pass pointer vs address of pointer (double pointer confusion)

So I had this task of swapping the values of two pointers themselves. The question said that i should i make it so that the function takes as an input the address of the pointer variables and then swaps em. This is how i did it
#include<iostream>
using namespace std;
void swap(int **ptra, int **ptrb)
{
int* temp = *ptra; // declares a temp pointer to hold the value of pointer a
*ptra = *ptrb; // pointer a is given the value of pointer b
*ptrb = temp; // pointer b is given the value of temp (which had the value of pointer a)
}
int main(){
int a=5, b=10;
int *pa=&a; //pa and pb are pointer variables of type int.
int *pb=&b;
int **ppa=&pa; //ppa and ppb are called double pointers or pointers-to-pointers.
int **ppb=&pb;
cout << "before" <<pa<< endl;
cout << "before" <<pb <<endl;
swap(&pa,&pb);
cout << pa<<endl;
cout << pb<< endl;
}
I am having trouble understanding why the swap function works returns same output if i put pa or &pa.
Passing pointer-to-pointer or passing address of pointer are giving the same result because pointer-to-pointer is also storing the address of pointer.
so passing address directly using & or passing pointer will give the same output.
Pointer: A pointer is a variable that stores the memory address of another
variable
int a = 5;
int *ptr = &a;
int **pptr = &ptr;
std::cout << pptr; // pptr: 0x77bcdff7d0
std::cout << &ptr; // &ptr: 0x77bcdff7d0
pointer-to-pointer is defined as int **ppa = &pa so swap(&pa, &pb) and swap(ppa, ppb) will give the same ouput

I am getting a issue in copying a pointer to another pointer in c++

I am getting segmentation fault in first code but the second code is running fine, don't know how?
How can I copy a pointer and save it to another pointer?
#include <iostream>
using namespace std;
int main() {
int *p;
int *p1;
*p1=7;
p=p1;
cout<<*p<<" "<<p;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int *p1;
*p1=7;
int *p=p1;
cout<<*p<<" "<<p;
return 0;
}
//7 0x7ffeea73db70
Both cases invoke undefined behaviour, in both cases you make use of uninitialized pointer p1, the fact that the second case "works" for you is a matter of sheer luck, as you can see here.
For your code to be valid you need to make it point to a valid memory address either by allocating memory manually:
int *p1 = new int; //raw pointer, (better to use smart pointers* but let's not get ahead of ourselves).
Or by assigning it the address of a valid int variable:
int i = 5;
int *p1 = &i;
How can I copy a pointer and save it to another pointer?
A pointer is essentially a variable like any other, you can copy it like you do a normal primitive variable, in fact you do just that when you do p = p1, these are two different pointers that will now contain the same value, the address of the variable they point to.
This code exemplifies this
#include <iostream>
using std::cout;
using std::endl;
int main() {
int *p1 = new int;
*p1 = 7;
int *p = p1;
cout<< "Value stored in the address p points to: " << *p << endl
<< "Value stored in the address p1 points to: " << *p1 << endl
<< "Address where p points to: " << p
<< " " << endl << "Address where p1 points to: "<< p1
<< endl << "Address of p: " << &p << endl << "Address of p1: "<< &p1;
return 0;
}
The output:
Value stored in the address p points to: 7
Value stored in the address p1 points to: 7
Address where p points to: 0x804150
Address where p1 points to: 0x804150
Address of p: 0x7ffc9447e220
Address of p1: 0x7ffc9447e228
*What is a smart pointer and when should I use one?
When you dereference an int* pointer, you promise that there is an int object at that address. C++ believes you, often without questioning. But you never wrote int a; p1=&a; or any other code that made sure p1 points to an actual int.
In fact, p1 isn't even a null pointer. The only thing you can do with p1 is to assign it a legal value. That is to say, it must appear at the left-hand side of an assignment first. It can't be used on the right hand side in int* p = p1;

Pointer will not return the address of a char type

Here is my code:
using namespace std;
int b = 25;
char x = 'x';
int* p = &b;
int* p2 = &x;
int main()
{
cout << "Pointer p is pointing to the address of variable b: " << p << endl;
cout << "Pointer p2 is pointing to the address of variable x: " << p2 << endl;
return 0;
}
This code will not give me the address of the variable x. It does not compile. It gives me an error as below:
error: cannot convert ‘char*’ to ‘int*’ in initialization
If I run this code with only the integer type I get a hexadecimal address of the variable a.
I am confused because supposedly, int* p2 = &x; should create a integer pointer to the integer address of the single char 'x' as well.
So why can I get the address of a integer but not the address of a char type???
The behaviour of your code would be undefined: your friendly compiler is helping you.
&x is a char* type pointing to a char object, and attempting to convert that to an int* type is a violation of the strict aliasing rule.
One fix would be to write char* p2 = &x; and (const void*)p2 in the corresponding std::cout statement to circumvent the calling of the special overload that std::cout has for const char* types.

C++ How to properly copy the value of a pointer

So I'm having a bit of an issue, and need help with this beginner question.
I've changed my example so that it makes sense. I should not have posted one while sleep deprived.
I'm trying to copy a pointer (lets call it p1) in order to create a second pointer (lets call it p2).
When p1 changes the object it is pointing at, I don't want p2 to change along with it, which is what appears to be happening in my program.
Essentially, here is the issue I'm having....
#include <string>
#include <iostream>
int main(){
std::cout << "Pointer Test\n\n";
int num1, num2;
int * p1, * p2;
num1 = 5;
num2 = 10;
p1 = &num1;
p2 = p1;
std::cout << "P1 is pointing at value " << *p1 << " (should be 5)\n";
std::cout << "P2 is pointing at value " << *p2 << " (should be 5)\n\n";
*p1 = num2; //Even though the value of P1 is changing, I don't want the value of P2 to change. I want it to stay on num1
std::cout << "P1 is pointing at value " << *p1 << " (should be 10)\n";
std::cout << "P2 is pointing at value " << *p2 << " (I want this to be 5, not 10)\n";
return 0;
}
/*
Assignments I've tried to make this work...
p2 = p1; //didn't work - *p2 changes when *p1 does, which I am trying to avoid
*p2 = *p1; //didn't work - caused segfault
*p2 = p1; //didn't work - can't convert int to *int, so won't compile
p2 = &(*p1); //didn't work - gave same result as p2 = p1
*/
Any enlightenment on this matter would be greatly appreciated.
I think you have a misunderstanding of what is going on. The statement *p1 = num2; is not changing the value of p1, it is changing the value of whatever p1 points to. Since both p1 and p2 point to the same variable when you change its value through one pointer you will see the same value when you dereference either pointer. What you want to do is have p1 point to num2, not take the value of num2 and store it in whatever p1 points to.
Change
*p1 = num2;
to
p1 = &num2;
Consider the following code:
int a = 5, b = 10, *foo, *bar;
foo = &a; // foo now points at the address of A
bar = foo; // bar now points at the address at which foo is pointing
foo = &b; // foo now points at the address of B
cout << *foo; // prints 10
cout << *bar; // prints 5
Thats the behaviour you want to achieve if Im not mistaken.
Your code is nonsensical however. IF I'm not wrong, your mistake is that you create a second pointer which points at the address of the first pointer instead of pointing at the address from the first pointer.
Your first code makes no sense. If testObj isn´t a typedef to a pointer,
the * in the first p1 assignment is wrong (it should be &), and the second p1 assignment is wrong too, because you can´t assign a temporary object to a pointer, (only addresses to variables or from new).
And always remember to assign an address to a pointer p before using *p
If you want to copy pointers, the copy will point to the same thing as the original one and p2 = p1; is correct. If you want to copy objects, the pointer thing has nothing to do with it, and how the object is copied should be defined in a copy constructor and operator=.
Depending on the class, memcpy can work or not, don´t rely on it for such cases.
Nonsensical code aside
When p1 changes the object it is pointing at, I don't want p2 to change along with it
If p1 and p2 point to the same object, changing the object p1 points to will change the (same) object p2 points to, period. You'll have to use a different mechanism than raw pointers to avoid that behaviour. Like making copies of the object pointed to instead of copying the pointer.
Also
Do I have to use memcpy? Or is there another way?
Since this is C++, I'd avoid memcpy.
Defining a copy constructor or assignment operator is a better way of copying objects.
There is no type like obj *. The "*" binds tighter to "p1". So you need to change the decl from
obj * p1, p2;
To
obj *p1, *p2;

Can a Pointer variable holds the address of another Pointer Variable?

Is it possible to make a pointer variable hold the address of another pointer variable? eg:int a;
int *ptr,*ptr1;
ptr=&a;
ptr1=&ptr;
Sure, a pointer to a pointer.
int i;
int *pi = &i;
int **ppi = π
There is nothing particularly unusual about a pointer to a pointer. It's a variable like any other, and it contains the address of a variable like any other. It's just a matter of setting the correct type so that the compiler knows what to do with them.
Yes, but it needs to have the right type. In your example int *ptr,*ptr1; both ptr and ptr1 have type "pointer to int", which can only point to an int, not a pointer. If you declare int *ptr, **ptr1;, then ptr1 has type "pointer to int *" and thus can point to ptr.
Here's a sample showing what happens
int a = 13;
int *ptr,
int **ptr1; // ** means pointer to pointer
ptr = &a;
ptr1 = &ptr;
cout << a; //value of a
cout << *ptr; //ptr points to a, *ptr is value of a
cout << **ptr1; //same as above
cout << &ptr; //prints out the address of ptr
cout << *ptr1; //same as above
It works the same for int ***ptr, int ****ptr.
Pointer to pointer is possible (and very common), but int* may not be large enough to contain the address of another int*. use int**. (or void*, for generally pointer)
There's two answers here and they're both yes.
Two pointers can point to the same location
int b, *p1=&b, *p2=&b;
*p1 = 123;
*p2; // equals 123
You can also have a pointer to a pointer:
int x=2, y=3, *p=&x, **q=&p;
Note the extra asterisk.
**q; // equals 2
*q = &y;
**q; // equals 3
**q = 4;
y; // equals 4
Yes,
Pls see the following code. I hope it will serve your purpose
int a = 4;
int *ptr = &a;
int *ptr1 = (int*)&ptr;
cout << **(int**)ptr1;
Here ptr1 is single pointer but behaves as double pointer