I am a beginner in C/C++ and i am trying to learn the pointers.
Here is my Code to create the array of pointers with each element in the pointer array, pointing to the element in the data array:
#include <iostream>
using namespace std;
//Pointers reference article
//https://www.programiz.com/cpp-programming/pointers-arrays
/* Array of pointers */
const int MAX = 5;
int main(){
int arr[MAX] = {1,2,3,4,5};
int* ptr[MAX];
cout << "Create the handle of each element in data array to the ptr array: " << endl;
for (int i=0; i<sizeof(arr)/sizeof(arr[0]);i++)
{
ptr[i] = &arr[i];
cout<<"ptr["<<i<<"] = " << ptr[i] << endl;
}
cout << "Display the contents of array using 1:1 ptr array:"<< endl;
for (int i=0; i<sizeof(arr)/sizeof(arr[0]);i++)
cout<<"arr["<<i<<"] = " << *ptr[i] << endl;
system ("pause");
return 0;
}
The above program works as expected. But, if i change the pointer type from int to void during pointer declaration, i.e from int* ptr[MAX]; to void* ptr[MAX];
I have this error: cpp(22): error C2100: illegal indirection
Line 22: cout<<"arr["<<i<<"] = " << *ptr[i] << endl
Can someone please educate me on this error. Thanks in advance.