0
C++ pointer to pointer
Please explain to me what Will be the output of this code below? Suppose all header file included, namespace is bring used? int a = 5; int *ptr1; int *ptr2; ptr1 = ptr=2; cout<<a<<*ptr1<<*ptr2<<endl; cout<<ptr1<<ptr2<<endl; cout<<&ptr1<<&ptr2;
7 Respostas
+ 1
This is a latest problem on sololearn code change, which said that the code above is not true, not valid, cannot be run or compiler. In my opinion, sololearn should recheck their code challenges problem and verify they give the right answer to the code challenges.
0
Actually it will not give any o/p instead it will throw an error.....
'error: invalid conversion from int to int*'
Which means you are trying to assign an int to an int pointer that is invalid.
ptr1 = ptr2 = 2; //error
Now lets modify your code like below....
int a = 5;
int *ptr1;
int *ptr2;
*ptr1 = *ptr = 2;// modified
cout<<a<<*ptr1<<*ptr2<<endl;
cout<<ptr1<<ptr2<<endl;
cout<<&ptr1<<&ptr2;
Interesting thing is that even the above modified code still throws an error.....
'error: command dumped core(segmentation fault)'
Continued.......
0
This error is occurring bcz of uninitialized pointer variable.
int *ptr1;// uninitialized, points to invalid memory location
See below for more info....
Uninitialized Pointers
The following code fragment has a serious logical error!
int * iPtr;
*iPtr = 55;
cout << *iPtr << endl;
The pointer iPtr was declared without initialization, i.e., it is pointing to "somewhere" which is of course an invalid memory location. The *iPtr = 55 corrupts the value of "somewhere"! You need to initialize a pointer by assigning it a valid address.
Continued.....
0
Now you have the idea how to get the correct code....
Initialize your pointer to a valid memory location with the help of address of(&) operator like below..
int b = 2;
int *ptr1 = &b;
Solution is attached below...
https://code.sololearn.com/cGobXZvUn9O7/?ref=app
Note:- &ptr1 points to the address where int pointer ptr1 is stored.
I hope, i have helped you ......
Thanks....
0
For more information on pointers.......
Refer to the link below.....
https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp4_PointerReference.html#:~:text=This%20is%20dangerous!,assigning%20it%20a%20valid%20address.&text=The%20address%2Dof%20operator%20(%20%26%20)%20operates%20on%20a%20variable,address%20of%20the%20variable%20number%20.
0
Thank you Rookie.
But I have different opinion.
Declare :
int *ptr; // unitialized.
It jeans this pointer is not yet set to point' to specific location (actually the specific location is another int variabel, said int a=5;
Wunitilized arise because it not confirm to c++ latest standard. It is a warning only.
Now if we defined int *b = 10;
cout<<b: Will output the address referred by *b, which is actually memory address occupied by int a itself. It is clear that cout<<b Will give same output to cout<<&a;. While cout<<*ptr Will give an output identical to cout<<a; itself.
Next, we defined int *c; then set c to point' to b. we add new command line: c=b; // not *c = *b; you can try later.
if we write:
cout<<b<<c, it Will output a memory address pointed by *b pointer which is the location of memory occupied by int variable a.
0
now if you write cout<<*b<<*c does the compiler will output the value store in a Twice: 55 or, it Outputs 50x xxx hex which indicates that *c pointer stores the address of *b, while b stores address of integer variable a in the computer memory, or the value of integer a which is stored in variable a or at the specific memory location occupied by int variable a which is 5?.
if I have:
int a = 5;
int *ptr1;
ptr1 = &a;
int *ptr2;
ptr2 = ptr1;
cout<<*ptr1<<*ptr2;
This the code above is valid or true. Isn't it?