Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: Leo the 28C (by Sulfurik)
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.cxRuffle Mayo says ROFLMAO! :D

You cannot return an array that way, you need to pass it to the function by reference.
Chi
They mostly come at night...mostly.

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.cxRuffle Mayo says ROFLMAO! :D

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.

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.

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...!"

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.cxRuffle Mayo says ROFLMAO! :D

/*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

Ooohh... I see... thanks! :-D
http://sulfurmidis.com/sulfursoft
http://tsfc.ath.cx
ftp://tsfc.ath.cx
hotline://tsfc.ath.cxIf I connect my microwave to my PC, will I be able to download food?

![]() |
![]() |
![]() |

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