C++ pointer | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C++ pointer

suppose I have: int a=100; int *ptr; ptr = &a; // set ptr to point' to the address in //memory occupied by int a. cout<<&ptr;<<endl; cout<<ptr<<endl; Do you know that statements cout<<&ptr. and. cout<<ptr respectively, both will give us the same result or output? Both line Will output the memory address occupied by integer a variable that the pointer ptr is currently pointing to?

9th Aug 2021, 11:33 PM
Oliver Pasaribu
Oliver Pasaribu - avatar
5 Answers
+ 2
No they won't "&ptr" will be the memory location of the pointer itself. "ptr" will be the content of the variable of pointer type, this means it will contain the address of "a" in this case.
10th Aug 2021, 12:40 AM
Arsenic
Arsenic - avatar
10th Aug 2021, 1:23 AM
Abhishek Kumar
Abhishek Kumar - avatar
0
#include <iostream> using namespace std; int main() { int a =10; int *ptr; ptr = &a; cout<<&a<<endl; cout<<&ptr<<endl; cout<<ptr<<endl; return 0; } Please check this one. Cout<<&a and cout<<p: both will return the memory address which is occupied by integer variable a or in the other word it return the location of memory address which is bring pointed by pointer to integer variable *ptr (it is set to point' to memory address location occupied by integer variable a which is store int value 100 , while cout<<&ptr itself will return the memory address occupied by int *ptr (not memory is pointed to).
10th Aug 2021, 12:53 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Do you agree with my opinion or statement friends?
10th Aug 2021, 12:54 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Oliver Pasaribu what you said in comments is correct
10th Aug 2021, 2:43 AM
Rishi
Rishi - avatar