0

I know that you can pass a multidimensional array into a function using:

void Class1::foo(Bar bars[][10])
{
   // Do stuff
}

and that you could return a pointer to the first member in a single-dimensional array by using:

Bar* Clas2::getBars()
{
   return bars;  //Where bars is a member of a class
}

However when 'bars' is a multidimensional array, I get the error:

Cannot convert Bar (*)[10] to Bar* in return

Could someone clarify why this is occuring?

2 Answers 2

1

You should write as the compiler says

Bar (*)[10] Clas2::getBars()
{
   return bars;  //Where bars is a member of a class
}

You said correctly that "you could return a pointer to the first member in a .. array". The member or more precisely element of your two dimensional array is one dimensional array of type Bar [10]. The pointer to this element will look as Bar (*)[10]

Oh I am sorry indeed shall be as

Bar (* Clas2::getBars() )[10]
{
   return bars;  //Where bars is a member of a class
}

Or you can use typedef. For example

typedef Bar ( *BarPtr )[10];
BarPtr Clas2::getBars()
{
   return bars;  //Where bars is a member of a class
}
Sign up to request clarification or add additional context in comments.

3 Comments

When I do, I get the error: "unqualified-id before ')' token, and states that getBars() returns no type.
@Xemerau I updated my post. If you used a construction from the upadted post then what is the error and what construction did you use?
The error was coming from the solution you posted before the update, using typedef fixed it though. Thanks a ton.
0

You should use:

Bar (*Clas2::getBars())[10]
{
    return bars;  //Where bars is a member of a class
}

or better looking way:

typedef Bar (*Bar10x10ptr)[10];

Bar10x10ptr Clas2::getBars()
{
    return bars;  //Where bars is a member of a class
}

2 Comments

If I do this, i get an error indicating that 'getBars' is a declared as a function returning an array, which seems like what I wanted to do.
On GCC it works fine. Try to test it with int instead of Bar. Or you can try this: Bar (*a)[10]; decltype(a) Clas2::getBars() { return bars; //Where bars is a member of a class }

Your Answer

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.