Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Return arrays? (C++)

Original Message
Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 10:39:35 Pacific
Subject: Return arrays? (C++)
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


Report Offensive Message For Removal


Response Number 1
Name: Chi Happens
Date: September 22, 2005 at 10:57:25 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
You cannot return an array that way, you need to pass it to the function by reference.

Chi

They mostly come at night...mostly.


Report Offensive Follow Up For Removal

Response Number 2
Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 11:15:08 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Chi Happens
Date: September 22, 2005 at 11:20:41 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 4
Name: Chi Happens
Date: September 22, 2005 at 11:21:38 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 5
Name: Stephen Hall
Date: September 22, 2005 at 18:41:37 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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...!"


Report Offensive Follow Up For Removal


Response Number 6
Name: Leo the 28C (by Sulfurik)
Date: September 22, 2005 at 19:19:45 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 7
Name: sen (by santanusen_82)
Date: October 5, 2005 at 02:57:39 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
/*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


Report Offensive Follow Up For Removal

Response Number 8
Name: Leo the 28C (by Sulfurik)
Date: October 5, 2005 at 15:58:48 Pacific
Subject: Return arrays? (C++)
Reply: (edit)
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?


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Return arrays? (C++)

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC