Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

LordG

How to return an array in a function in C++

This topic is 6015 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can someone tell me how to return an array in a function in C++. Here''s an example of what I have so far... char *class::getArray() { char *name[10]; strcpy(name[0], "bob"); strcpy(name[1], "joe"); return name; } After I compile, I get some error saying "cannot convert `char**'' to `char*'' in return" Someone help me, this is for some C++ assignment I have to do.

Share this post


Link to post
Share on other sites
Advertisement
In your function, you build an array of ten character arrays. In other words, you''re making an array of arrays of characters.

When you return, however, you''re trying to return an array of characters (char*). Obviously, you can''t do this. What you should do instead is change the prototype to return a char**.

Share this post


Link to post
Share on other sites
To clarify, you can''t return an array - but you can return a pointer and arrays and pointers are very similar. The name of an array is equivalent to a pointer to the first element of the array. An array of arrays involves an extra level of indirection and so you need to return a pointer pointer to compensate.

Share this post


Link to post
Share on other sites
Even if you do that, you''ll have a problem. You''re trying to return a pointer to an object (name) that only "lives" within getArray; its scope is exclusive to that function. So, anywhere outside of there, any pointer you return to it will be invalid. One option is to dynamically allocate name, but that would force the caller to deallocate it, and that''s just very problematic. Probably the best way to solve this would be to make name a data member of your class so that as long as an instance of that class exists, its name member will be also.

Share this post


Link to post
Share on other sites
also, asigning values (strcpy) to unitialized char pointers is not a good idea, either new them, or declare name as:

char name[10][2];

but you will have to declare it as a global variable (in which case you wont really need to return it) since once the function is done (after return) the scope if the variable is too, and you should not rely on the returned value.

all that just to say, use new, and remember to delete when you''re done

  
char **class::getArray()
{
char **name = new *char[10];
name[0] = new char[strlen("bob")];
strcpy(name[0], "bob");
name[1] = new char[strlen("joe")];
strcpy(name[1], "joe");
return name;
}

void class::releaseArray(char **name)
{
if(name==NULL) return;
for(int i=0;i<10;++i)
{
delete [] name[i];
}
delete [] name;
}


I would also recomend making "name" a private (or protected) member variable so you save passing the pointer to releaseArray, and everything that belongs to the class stays inside the class

Share this post


Link to post
Share on other sites
The STL is great, but if you mean to have it returned by value, that would be bad. As a data member, that''d be great, and certainly preferable to any C-style strings.

LordG, it would, but there are potential issues with that, namely guaranteeing that there will only be one access at a time to that buffer if the buffer''s contents might get changed. If you determine that there will be up to two at once, you could make two buffers and use another static variable to cycle through the buffers so each accessor has its own buffer. Threading will make it even more problematic. So, I think it''s best just to have it as a member, where its simpler to synchronize if necessary.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by merlin9x9
The STL is great, but if you mean to have it returned by value, that would be bad. As a data member, that''d be great, and certainly preferable to any C-style strings.



Well, instead of this:


std::vector<int> function()
{
std::vector<int> v;

// Modify v

return v;
}


just do this:


void function(std::vector<int>& v)
{
// Modify v
}


Then you don''t waste time on a copy constructor.

By the way, did you know your allowed to return a pointer to a string literal? They never go out of scope. For example, this is guarenteed to work:

{code]
const char* function(int x)
{
if (x == 0)
return "zero";

return "non-zero";
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!