+ 2
confussed with the concept "pointers"
#include <iostream> using namespace std; int main() { int score = 5; int **scorePtr; int ptr; scorePtr = &score; ptr = **scorePtr; cout <<ptr << endl; return 0; } here i have written the code to print the value 5 bt im ending up with some erros can someone explain me what changes have to be done in the codding in order to print 5 using double pointer **
2 Answers
+ 1
When you put &score into scorePtr, you're treating it as a double pointer. That is, it dereferences the pointer to score (which is 5), then it tries to dereference what's at the memory address 5. You'll have to make a pointer to &score and put that into scorePtr to make your program work properly.
+ 1
#include<iostream>
using namespace std;
int main(){
int score = 5; //score is a variable of integer type
int *ptr; //ptr is a pointer to an integer
int **scorePtr; //scorePtr is pointer to pointer
ptr = &score; //address of score is assigned to ptr
scorePtr = &ptr; //addr of ptr is assigned to scorePtr
//scorePtr is a double pointer
cout << score << *ptr << **scorePtr ;
return 0;
}