Solved

Copying retruned array

Posted on 2003-12-01
5
602 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
c++ how to tell if the progra is ctl or mfc atl ect 6 98
C++ assignment question 7 267
C++ Language error 28 276
C++ help/ Toy problem 19 50
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  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 goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

740 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