Can a Pointer variable holds the address of another Pointer Variable? - c++

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

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

C++ pointers incorrect syntax? [duplicate]

This question already has answers here:
What is array to pointer decay?
(11 answers)
Closed 7 years ago.
I saw this code in this link-http://www.tutorialspoint.com/cplusplus/cpp_pointers_vs_arrays.htm. Look at the first piece of code.
#include <iostream>
using namespace std;
const int MAX = 3;
int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;
cout << "Value of var[" << i << "] = ";
cout << *ptr << endl;
// point to the next location
ptr++;
}
return 0;
}
Shouldn't it be ptr = &var instead of ptr = var? It is below the comment. Why is it declared simply asvar instead of &var?
var will decay into a pointer to its first element.
ptr = var;
is equivalent to
ptr = &var[0];
&var is a pointer to an array, not to an int.
int (*aptr)[MAX] = &var;
would be valid, but it doesn't mean the same - *aptr is an array with MAX elements, not an int.
The tutorial's claim that "pointers and arrays are interchangeable in many cases" is completely wrong.
The only time you can "interchange" anything is this case; when something expects a pointer, an array decays into a pointer to its first element.
There is not a single case where you can use a pointer in place of an array.
Confusingly, ptr is often called "a pointer to an array" or even "an array" in informal conversation, even though this is formally incorrect.
This is because "a pointer to the first element of an array" is quite a mouthful, and to a non-beginner it's usually clear from context what is actually meant.
An array "decays" to a pointer to its first element in assignments, parameter passing etc. That would be a pointer to int, which is what p is declared as.
You actually can take the address of the array, although it is much less common. The address then is the address of the whole array, like this:
int (*parr)[3] = &var;
Now cou can say, for example, int i = (*parr)[1]; to initialize i with the second element of var, 100.
The address of the whole array is numerically identical with the address of its first element because the first element is where the array starts, after all. That may seem funny: What's the point then?
The difference is in type, which (apart from mere grammar questions about type compatibility -- but you can always cast those away in C!) determines what happens when you do pointer arithmetics. Consider
int *ptr = var;
int (*parr)[3] = &var;
printf("ptr: %p, parr: %p\n", ptr, parr);
printf("Increment ptr: %p, incremented parr: %p\n", ptr+1, parr+1);

How to define an alias to a pointer to an integer?

I am trying to run the following code, but I am getting the following error.
error: cannot declare pointer to 'int&'
#include <iostream>
using namespace std;
int main()
{
int x = 5;
int *ptr = &x;
int &(*y) = ptr;
*y = 5;
cout << *ptr << endl;
return 0;
}
You declare references to pointers the same way you declare references to basic types.
Consider:
int main()
{
int i = 0; // int
int& ir = i; // int reference (reference to int)
int* ip = &i; // int pointer (pointer to int)
int*& ipr = ip; // int pointer reference (reference to pointer to int)
*ip = 5;
cout << *ipr << endl;
return 0;
}
If you just want a new pointer to the same region of memory, use:
int *y = ptr;
This not so much an "alias" in that if you change *ptr or *y, both will change, but if you change the pointers themselves, the other will not be updated.
If you actually do want a reference to a pointer, use:
int *&y = ptr;
int *ptr = &x;
pointer value has an address and a type of x.
when you typed code above, the value of ptr is an address of x, and ptr know the type of x.
int * (&y) = ptr;
the code above is declaring variable 'y' (type:int*, define:ptr's reference)
reference variables should be declared and defined simultaneously.
anyway, as a result, ptr and y are pointing same memory address.
you can easily think y is a nickname of ptr.
so you can access the variable 'x' by using y, instead of ptr.
v - a variable name.
&v - a variable that will be a reference of something.
*&v - a variable that will be a reference of a pointer to something
int *&v - a variable that will be a reference of a pointer to int
Or for a more interesting example,
(*&v)[5] - a variable that will be a reference of a pointer to an array of 5 something
int (*&v)[5] - a variable that will be a reference of a pointer to an array of 5 int

Not able to understand the notations : * and ** with pointers

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.

What is the int* here?

So I know that a pointer is:
int *someNumber;
but I have run accross some code with:
int* pOutputData = nullptr;
byte* pOutputDataByte = nullptr;
What does int* mean?
Is that some kind of array of int? Is it a pointer to the type?
int * and int* are the same thing. The whitespace doesn't change anything.
int * can be used either as a pointer to int or as a pointer to the first element of an array of ints. How a variable is used is context-dependent. Judging by the variable names, I'd guess that someNumber points to a single int and pOutputData points to an array.
Both are same
White space does not mean anything in c
int * num ;
int *num;
int* num;
All the above statements are same!
We use int* as default; mainly in data structure, to avoid confusion.
Always read pointer variables from right to left
int *p means p is a pointer to an int
char *p means p is a pointer to a char
const int *p means p is a pointer to an integer constant
int * const p means p is constant pointer to an int