0

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.

BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
sharan
  • 13
  • 1
  • 4
    If you dereference a `void *`, you just get a `void`. I'm not really sure what you would expect that to do. – Dolda2000 Jan 15 '17 at 04:04
  • 1
    There is no language C/C++. One the distinct languages C and C++. This code is clearly not C. – too honest for this site Jan 15 '17 at 04:04
  • 1
    The whole point of a `void` pointer is that it can point at (almost) anything, regardless of type, so the type of what it points at is unknown. To dereference a pointer (as in `*ptr[i]`, where `ptr[i]` is a pointer) the type of what it points at must be known. – Peter Jan 15 '17 at 04:04
  • 1
    @Dolda2000: You cannot dereference a `void *`! – too honest for this site Jan 15 '17 at 04:05
  • 3
    @Olaf: That was kinda my point. – Dolda2000 Jan 15 '17 at 04:05
  • 1
    Not related to the question, but I recommend declaring pointer variables as `int *x;` instead of `int* x;`, because the `*` modifies the declarator rather than the type. For instance, `int* a, b;` gives the impression that both `a` and `b` are `int` pointers, when in fact only `a` is while `b` is just an `int`. `int *a, b;` does somewhat better at avoiding that misunderstanding. (Though arguably `int *a; int b;` is the less confusing construction.) – Dolda2000 Jan 15 '17 at 04:08
  • 1
    @Dolda2000 Whatever declare more than one variable in a statement is also a bad practice so I'm not agree with your argument. – Stargateur Jan 15 '17 at 04:27
  • @Dolda2000 I would recommend the opposite because in `C++` the `*` is part of the type in a declaration (C++ is type-centric). This is what Bjarne Stroustrup recommends. – Galik Jan 15 '17 at 04:45
  • 1
    @Galik: I just tried, and using G++, `int* a, b` certainly declared `b` as an `int`, not an `int *`. – Dolda2000 Jan 15 '17 at 04:47
  • @Dolda2000 Not the place to debate it, its personal preference. But no one should be using that construct for several reasons. Even so the `*` is still part of the *type* information. This is what Bjarne suggests and that is what is used in the `C++` Standard documents. The old "expression centric" view is more favored by `C` programmers from what I can see. Also `int* p;` is more intuitive and easier to comprehend for beginners. Plus the "expression centric" view doesn't make sense for *references*. – Galik Jan 15 '17 at 04:56
  • 1
    @Galik technically the language grammar defines a declaration as a type specifier followed by a list of declarators, and the way the grammar is written, the `*` is part of the declarator, not the type specifier. But I don't think that has anything to do with the style decision. I have seen people advocate `int *p` but `int& q`. – M.M Jan 15 '17 at 05:01
  • BTW i wonder if there will ever be a question mentioning a pointer without somebody coming along to impose their personal style in comments – M.M Jan 15 '17 at 05:02

2 Answers2

4
int* ptr[MAX];

ptr is an array of pointer to int.

When you change to

void* ptr[MAX];

then ptr is an array of pointer to void. That does not cause an error on the the first cout

cout<<"ptr["<<i<<"] = " << ptr[i] << endl;  // ok - printing the address

But it's an error on the second:

cout<<"arr["<<i<<"] = " << *ptr[i] << endl;  // error - dereferencing void pointer

You cannot dereference a void pointer - that's what the error about. A pointer must be of specific type to be dereferenced.

artm
  • 17,291
  • 6
  • 38
  • 54
2

You need to ask yourself one question - What does void mean?

So you have a void pointer - this means it points in to the void

That can be anything - Any int, A structure, An object, A float....

Get the picture

Then the compiler is trying to de-reference it - So it holds up its hands and say - I have not got a clue.

Either case it - or better still Avoid void pointers

Ed Heal
  • 59,252
  • 17
  • 87
  • 127