Solved

Copying retruned array

Posted on 2003-12-01
5
598 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 80 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 25 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 20 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now