Computing.Net > Forums > Programming > Return arrays? (C++)

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Return arrays? (C++)

Reply to Message Icon

Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 10:39:35 Pacific
OS: Windows XP Home SP2
CPU/Ram: 2.8 GHz/448 MB
Comment:

Hello everyone! :-D
OK, I got a simple question, how do I make a function return an array? This gives me a compile error:

CHAR_INFO[] DrawMap()

How do I fix it? Thanks! ;)

http://www.xthost.info/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D



Sponsored Link
Ads by Google

Response Number 1
Name: Chi Happens
Date: September 22, 2005 at 10:57:25 Pacific
Reply:

You cannot return an array that way, you need to pass it to the function by reference.

Chi

They mostly come at night...mostly.


0

Response Number 2
Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 11:15:08 Pacific
Reply:

Hmm... I think I'll just use a global variable... Thanks! ;-)

http://www.xthost.info/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 3
Name: Chi Happens
Date: September 22, 2005 at 11:20:41 Pacific
Reply:

God NO!!!!
You should be using OOP and some elegance...not global variables.

Passing it by reference is dead simple.

just define your function as:
void DrawMap(char CHAR_INFO[])

Chi

They mostly come at night...mostly.


0

Response Number 4
Name: Chi Happens
Date: September 22, 2005 at 11:21:38 Pacific
Reply:

Here is an example:

#include "stdafx.h"
using namespace std;
void DrawMap(char CHAR_INFO[]);
int _tmain(int argc, _TCHAR* argv[])
{
char char_info[3] = {'1','2','3'};
DrawMap(char_info);
cout << char_info[0] << endl;
int a;
cin >> a;
return 0;
}

void DrawMap(char CHAR_INFO[])
{
CHAR_INFO[0] = 'A';
}

Chi

They mostly come at night...mostly.


0

Response Number 5
Name: Stephen Hall
Date: September 22, 2005 at 18:41:37 Pacific
Reply:

Sulfurik,

Are you creating the array inside the function? If so, you'll either need to use double indirection (a good old double pointer in C (**) ) or create the array inside the function and then return a pointer (pointers and arrays are pretty much the same thing in the compiler's eyes). Here's an example of both (only if you are creating the array _inside_ the function).

//double pointer
void DrawMap(char **array)
{
*array=(char *)malloc(100);

if(!(*array)){
return; //bail
}

(*array)[0]='A';
}

//return pointer
char *DrawMap(void)
{
char *array;

array=(char *)malloc(100);

if(!array){
return; //bail
}

array[0]='A';

return(array);
}

Otherwise, Chi is right. Sure it's kind of old fashioned (I really need to polish up on my techniques and get more C++ oriented, but I don't have time!), but it works! Hope this helps, and good luck once again on your project.

Stephen

"Live long and PROGRAM......or at least do _something_ with all that time...!"


0

Related Posts

See More



Response Number 6
Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 19:19:45 Pacific
Reply:

Oohh... Well... I'll use global variables for now, but then I'll go object-oriented (making a game engine!) when I finish this test game. Thanks to both of you! ;-)

http://www.xthost.info/sulfurik/
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

Ruffle Mayo says ROFLMAO! :D


0

Response Number 7
Name: sen (by santanusen_82)
Date: October 5, 2005 at 02:57:39 Pacific
Reply:

/*CHECK THIS OUT*/
char* arrayReturner( )
{
static char arr[20] = "Hello World";
return(arr);
}

main()
{
char *str;
str = arrayReturner( );
printf(str);
}

Santanu Sen
National Institute of Technology
Durgapur
India


0

Response Number 8
Name: Leo the 28C (by Sulfurik)
Date: October 5, 2005 at 15:58:48 Pacific
Reply:

Ooohh... I see... thanks! :-D

http://sulfurmidis.com/sulfursoft
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cx

If I connect my microwave to my PC, will I be able to download food?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Return arrays? (C++)

C++ returning arrays www.computing.net/answers/programming/c-returning-arrays/9763.html

__gc and returning a String array www.computing.net/answers/programming/gc-and-returning-a-string-array/6877.html

C++ major probs www.computing.net/answers/programming/c-major-probs/11647.html