Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

How do I initialize a pointer to an array of pointers?

I am trying to initialize a pointer to an array of pointers. Here's an example:

class MyClass { };
// INTENT: define a pointer to an array of pointers to MyClass objects
MyClass* (*myPointer)[];
int main(void) {
 // INTENT: define and initialize an array of pointers to MyClass objects
 MyClass * tempArray [] = {
 new MyClass(),
 new MyClass()
 };
 // INTENT: make myPointer point to the array just created
 myPointer = tempArray;
}

When I compile this I get:

./test2.cpp: In function ‘int main()’:
./test2.cpp:15:19: error: cannot convert ‘MyClass* [2]’ to ‘MyClass* (*)[]’ in assignment
 myPointer = tempArray;
 ^~~~~~~~~

Answer*

Draft saved
Draft discarded
Cancel
1
  • BINGO Thank you. This is exactly what I needed! Commented Dec 9, 2019 at 5:52

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /