Can you return an array?

Started by
12 comments, last by Fruny 19 years, 2 months ago
I am just messing around trying to get numbers from a file. What i was doing was

char load( char* ptr )
{
    
    ifstream fin;
    
    char map[5];
    
    fin.open(ptr);
    
    fin>>map;
    
    fin.close();
    
    return map;
    
}


char test;
    
    test = load("Test.txt");
    
    cout<<test;

Any suggestions ?
Advertisement
You are returning an object created within a certain scope. Returning the array will point to memory that you should no longer try to read / write. Create the array on the heap, but then this leads to who is responsible for cleaning up the memory.
What you could do is have one of the parameters as a pointer to your string and do this:

char load( char* ptr, char **out ){        ifstream fin;        char map[5];        fin.open(ptr);        fin>>map;        fin.close();    strcpy( *out, map );}char *ret = new char [length];load( "Funkytown", &ret );delete [] ret;
disregard me...
The man within the man. The love within the love. The sin within the sin.
No. You can't return arrays.

Quote:void load( char* filename, char* out )
{
ifstream fin(filename)
fin >> out;
}


Quote:string load( char* filename )
{
ifstream fin(filename)
string out;
fin >> out;
return out;
}


Quote:void load( char* filename, string& out )
{
ifstream fin(filename)
fin >> out;
}


etc
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Noir
so you must fix that function to:

char *load( char *ptr)
{
...
}


Returning a pointer to a local variable ... recipe for disaster.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
std::string is your friend:

#include <fstream>#include <iostream>#include <string>using namespace std;std::string load(std::string filename){    ifstream fin;    fin.open(filename.c_str());    string map;    fin >> map;    fin.close();    return map;}int main(){	string a;	a = load("Test.txt");	cout << a;	cin.get();	return 0;}

...Fixed. Thanks for noticing.

[Edited by - Rob Loach on February 15, 2005 4:45:26 PM]
Rob Loach [Website] [Projects] [Contact]
Thanks for all the help so far! ;). I was trying that last submission and i get...
11 C:\Dev-Cpp\Cpp CodeTest\fileinput.cpp
conversion from `const char*' to non-scalar type `std::basic_ifstream<char, std::char_traits<char> >'
requested
Quote:Original post by mcgrane66
Thanks for all the help so far! ;). I was trying that last submission and i get...
11 C:\Dev-Cpp\Cpp CodeTest\fileinput.cpp
conversion from `const char*' to non-scalar type `std::basic_ifstream<char, std::char_traits<char> >'
requested


The constructor is explicit. You need to write ifstream fin(filename.c_str());
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Sound, works great ;)
But can anyone now tell me, can i return a 2d array?
I can see how using strings is good for a 1d array - but what about a 2d?

This topic is closed to new replies.

Advertisement