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[KENO_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?
 
dkim18Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
int main()
{
 >>>>>  int *temp_array[80];

    char generated_data_file[] = "Keno_TC_2.dat";

    temp_array = Read_Input_File(generated_data_file);
    return 0;
}

By the above line, You are declaring an array of 80 int pointers

and u are retuning an array of integers from your function

So, in main, you should be declaring

int *temp_array ;

This means that temp_array is a pointer to integer, As u know in C/C++, a pointer can be treated as an array and vice versa with some limitations,

HTH

Amit

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
travdCommented:
There are a few problems with your code.

First, the function Read_Input_file returns a pointer to an array of integers (type int*), but in main() you are assigning it to temp_array which is an a pointer to an array of pointers to ints (int **).  This will be flagged as a type mismatch by your compiler.

You likely want to define temp_array as:

int temp_array[80]

The second problem is that the semantics of passing arrays and returning arrays is a bit different than normal values.  When you pass an array to a function, the actual array is accessed by that function, rather than a copy.  Similarly, if you "return an array" from a function, it actually "passes" back the real data.  The problem in your example is that the lifetime of the keno_num_arrange_array is over when the function returns, so that the memory can't be used by the main function.

Third, you can't assign arrays to each other:

int array1[10];
int array2[10];

array1 = array2; // not valid

What you want to do is to pass the temp array to the readfile function and modify it directly.  You don't need the keno_num_arrange_array at all:

void Read_Input_File(char* generated_data_file, int *array)
{

    const int KENO_NUM_SIZE = 80;
...
    do stuff here with array that you need to do with keno_array
...
     return;

 }

int main()
{
   int *temp_array[80];

    char generated_data_file[] = "Keno_TC_2.dat";

    temp_array = Read_Input_File(generated_data_file, temp_array);
    return 0;
}


You need some way to keep track of the array sizes since your function doesn't know the size of the array, etc etc.
travdCommented:
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.
dennis_georgeCommented:
hi,

 If you want to return an array make sure that it is not created on stack, because the scope of the local variable is the function boudary.... If you really want to return an array do the following steps
* Allocate the memory of the array in Heap
* return the address of that location
* In the calling function accept as a pointer...
* Don't forget to deallocate the memory assigned to the array in the function.

int *Read_Input_File(char* generated_data_file)
{

    const int KENO_NUM_SIZE = 80;
    int *keno_num_arrange_array = new int[KENO_NUM_SIZE]; // allocate memory on heap

     return  keno_num_arrange_array;

 }
 
int main()
{
   int *temp_array;

    char generated_data_file[] = "Keno_TC_2.dat";

    temp_array = Read_Input_File(generated_data_file);

    delete []temp_array ; // deallocate the memory on Heap
    return 0;
}

hope you got this...
Dennis
DanRollinsCommented:
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
     }
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.