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?
 
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
0
 
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.
0
 
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.
0
 
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
0
 
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
     }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.