• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Function with Array as Return Value


I'm using Managed C++ (.net) and have trouble in defining a function which returns an array.

I tried:

String* returnFunction()
  String* strArr[] = new String*[3];
  return strArr;

That didn't work.

Actually, I feel that something's missing but I really don't know what. I hope you can help me out with this.

Reminder: The Code should be usable with MC++
  • 3
  • 2
  • 2
  • +1
1 Solution
we just had a whole big post on this dealing with char arrays
what your doing may or may not be right, i can't tell because you put
in where there is code that could mae this right.
first off this is not the best programming practice, unless you HAVE to do this. the reason is cause you have to remember to delete the pointers outside of the function now.
here what you should do
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;

string** ReturnFunction()
     string **strarr = new string*[3];
     strarr[0] = new string("hello");
     strarr[1] = new string("world");
     strarr[2] = new string("of c++");

     return strarr;


int main(void)
     string **retstring;
     retstring = ReturnFunction();


     delete retstring[2];
     delete retstring[1];
     delete retstring[0];
     delete[] retstring;
    return 0;


first problem: return type
   a 2-dimensional array is not to be returned as string*
but string **
second: im not sure if you set each strarr[#] to a new string, you should have but that might have been in [..]
if you can it's better to pass the string array in
make a 2-dimensional array in main then pass it into the function
so instead of returning that type you add it as a parameter and return void or success/fail int
trojaAuthor Commented:
Thanks for the suggestions.

It seems like there is a misunderstanding. Actually this represents a one-dimensional array of Strings with 3 members. So you can replace [...] by e.g.

for (i=0; i<3; i++)
   strArr[i] = S"sameString";

But I really need to work this with managed C++, your example uses the standard library.

I'm considering doing it another way, passing a reference to the array as function parameter and changing the array there.

So can anyone tell me how to pass an array as reference in managed C++?
Here's an example of passing a reference to an array.

void SomeFunctiopn(int (&MyVariableName)[4])
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

trojaAuthor Commented:
All those examples don't work in the .net environment. Please provide code which uses C++ with managed extensions (MC++), not unmanaged code.

I have a System::String* Array and want to pass it as a reference to a function and change its values inside the function.

Thanks for replies
>> All those examples don't work in the .net environment.

Then perhaps you should go to the .NET topic area.

trojaAuthor Commented:
@ Exceter

I assume you won't find many C++ Programmers there. Most of the topics relate to ASP.NET, VB.NET and C#.

By the way, MC++ is still C++, it only has additionally managed extensions.

And last, if I already state that I need code that works in .Net, i don't see the reason why everyone gives me solutions to standard c++. I already know how to do it in standard c++, I was asking for mc++! Is that so difficult?

No, it's not. I already got the answer in a newsgroup.

@ PlanetCPP
But your answer would have done it, too, if I would have seen it earlier. Thanks. (I really have to take a better look at codeproject next time. You can find answers to most of the questions here in experts-exchange :-) )

>> i don't see the reason why everyone gives me solutions to standard c++.

Because this is the standard C++ topic area. :-)


Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now