Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Copying retruned array

Posted on 2003-12-01
5
Medium Priority
?
608 Views
Last Modified: 2012-06-27
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?
 
0
Comment
Question by:dkim18
5 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 320 total points
ID: 9856062
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
 
LVL 1

Assisted Solution

by:travd
travd earned 100 total points
ID: 9856067
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
 
LVL 1

Expert Comment

by:travd
ID: 9856073
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
 
LVL 5

Assisted Solution

by:dennis_george
dennis_george earned 80 total points
ID: 9856112
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 9856721
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question