2

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;
 ^~~~~~~~~
asked Dec 5, 2019 at 21:28
6
  • The compiler is complaining because myPointer and tempArray are different types. You can see this clearly in the declarations. You need to declare them both as the same type in order for the assignment to work. Commented Dec 5, 2019 at 21:31
  • Or you need to change the assignment statement. Perhaps myPointer = &tempArray is what you want? Commented Dec 5, 2019 at 21:33
  • 2
    Try using std::vector<MyClass *>. Vectors are a lot easier to deal with. Commented Dec 5, 2019 at 21:34
  • I want to avoid vectors because this is an embedded project. Commented Dec 5, 2019 at 21:41
  • FYI - embedded is not a good excuse to avoid STL and containers... Commented Dec 5, 2019 at 22:42

3 Answers 3

2

First, arrays are not pointers. You have to use the address-of:

 myPointer = &tempArray;

Next, when you write

T foo[] = { t1, t2 };

this is just short-hand notation for

T foo[2] = { t1, t2 };

Either use the correct type (Live Example):

MyClass* (*myPointer)[2];

or perhaps better use a std::array<MyClass> in the first place. And forget about raw owning pointers. Use smart pointers instead.

answered Dec 5, 2019 at 21:34
Sign up to request clarification or add additional context in comments.

4 Comments

@NickReed fixed. I had to combine two wrong answers to get a correct one ;)
@NickReed That repl doesn't appear to be changed from the OP's code here.
Solid, have an upvote! Out of curiosity, would you happen to know why can't one convert between foo[] and foo[2]? I was trying to answer the question myself, but couldn't find the reasoning behind the compiler's hangup about that.
@NickReed i have to admit, I have no clue what a foo[] (or MyClass* (*myPointer)[];) actually is
1
// INTENT: define a pointer to an array of pointers to MyClass objects
MyClass* (*myPointer)[];

You can't obmit the size of the array in declaration. You need this:

MyClass* (*myPointer)[2];

Then,

 MyClass * tempArray [] = {
 new MyClass(),
 new MyClass()
 };
 // INTENT: make myPointer point to the array just created 
 myPointer = &tempArray;
answered Dec 5, 2019 at 21:44

Comments

0

An array decays into a pointer to the first element. Since the element type is also a pointer, you can declare a variable that is a double-pointer and have it point at that element, eg:

class MyClass { };
MyClass** myPointer;
int main(void) {
 MyClass * tempArray [] = {
 new MyClass(),
 new MyClass()
 };
 myPointer = tempArray;
 // same as:
 // myPointer = &tempArray[0];
 ...
}
answered Dec 5, 2019 at 22:20

1 Comment

BINGO Thank you. This is exactly what I needed!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.