pointers program is giving errors

I am a student of class 12 having computer science as elective. I was making a test program using pointers. According to me it should display 5. But it is giving errors which I am unable to figure out why. Kindly help. I program on turbo c++ (I know it is too old but it is prescribed in our course).

This is my program.
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream.h>
#include <conio.h>

void main()
{
   clrscr();
   int a = 5;
   int* ptr = &a;
   int* ptr2 = &(ptr);
   cout<<**ptr2;
   getch();
}
Line 9 you are using the & operator on a int* pointer. This will give you a int** (pointer to pointer to int) but the type of ptr2 is different so you can't assign this value to it.
Line 9 should be
int* ptr2 = ptr;
Line 10 should be
cout << *ptr2;
Topic archived. No new replies allowed.