?
Solved

Function with Array as Return Value

Posted on 2003-03-01
8
Medium Priority
?
252 Views
Last Modified: 2010-04-01
Hi,

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++
0
Comment
Question by:troja
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8049941
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();

     cout<<retstring[0]->data()<<endl;
     cout<<retstring[1]->data()<<endl;
     cout<<retstring[2]->data()<<endl;

     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
0
 
LVL 1

Author Comment

by:troja
ID: 8050155
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++?
0
 
LVL 30

Expert Comment

by:Axter
ID: 8051043
Here's an example of passing a reference to an array.

void SomeFunctiopn(int (&MyVariableName)[4])
{
}
0
Industry Leaders: 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!

 
LVL 1

Author Comment

by:troja
ID: 8052044
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
0
 
LVL 6

Accepted Solution

by:
PlanetCpp earned 280 total points
ID: 8052841
0
 
LVL 8

Expert Comment

by:Exceter
ID: 8053089
>> All those examples don't work in the .net environment.

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

Exceter
0
 
LVL 1

Author Comment

by:troja
ID: 8053806
@ 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 :-) )

0
 
LVL 8

Expert Comment

by:Exceter
ID: 8055047
>> i don't see the reason why everyone gives me solutions to standard c++.

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

Exceter
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question