dkim18
asked on
Copying retruned array
I am trying to return array to main and copy to another array.
-------------------------- ------
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int *Read_Input_File(char* generated_data_file)
{
const int KENO_NUM_SIZE = 80;
int keno_num_arrange_array[KEN O_NUM_SIZE ];
...
...
return keno_num_arrange_array;
}
int main()
{
int *temp_array[80];
char generated_data_file[] = "Keno_TC_2.dat";
temp_array = Read_Input_File(generated_ data_file) ;
return 0;
}
--------------------
why am I having errors?
--------------------------
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int *Read_Input_File(char* generated_data_file)
{
const int KENO_NUM_SIZE = 80;
int keno_num_arrange_array[KEN
...
...
return keno_num_arrange_array;
}
int main()
{
int *temp_array[80];
char generated_data_file[] = "Keno_TC_2.dat";
temp_array = Read_Input_File(generated_
return 0;
}
--------------------
why am I having errors?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Beat me by one minute. But note that it is not valid to use the variable keno_num_arrange_array in the main function since it has automatic storage duration and is destroyed on function exit.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think all of the above are correct. However, the easiset way to to *not* return an array to main... rather create an array in main, and pass a pointer to that array to Read_Input_File():
bool Read_Input_File( char* szFile, int* anData ); // declare before using
void main()
{
int anData[80];
for (int j=0; j<80; j++ ) {
anData[j]= 0; // make sure all elements are zero
}
bool fRet= Read_Input_File( "c:\\Mydir\\Myfile.txt", anData ); // same as &anData[0]
for (int j=0; j<80; j++ ) {
cout << "Array index:" << j << " array item value: " << anData[j] <<endl;
}
}
bool Read_Input_File( char* szFile, int* anData )
{
... open the file ... see http:/Cplusplus/Q_20813330.html#9854643 for details
... read each line
... reach each datum into a string
... convert each string to a bunary int value...
int nValue= atoi( p );
if (nValue > 79 ) {
// this is an error, handle it
}
else {
anData[ nValue ]++; // indicate you got one more of these
}
}
bool Read_Input_File( char* szFile, int* anData ); // declare before using
void main()
{
int anData[80];
for (int j=0; j<80; j++ ) {
anData[j]= 0; // make sure all elements are zero
}
bool fRet= Read_Input_File( "c:\\Mydir\\Myfile.txt", anData ); // same as &anData[0]
for (int j=0; j<80; j++ ) {
cout << "Array index:" << j << " array item value: " << anData[j] <<endl;
}
}
bool Read_Input_File( char* szFile, int* anData )
{
... open the file ... see http:/Cplusplus/Q_20813330.html#9854643 for details
... read each line
... reach each datum into a string
... convert each string to a bunary int value...
int nValue= atoi( p );
if (nValue > 79 ) {
// this is an error, handle it
}
else {
anData[ nValue ]++; // indicate you got one more of these
}
}