new array in a function
by bkelly from LinuxQuestions.org on (#55ENJ)
My app needs to call a function that will read some number of values from a text file and put them in the array. The count is not known until the function opens the file. The array must be new'ed within the function. What is the syntax that allows the new'ing of the array within the function. Below are some code fragments showing the concept. I know the syntax is wrong but cannot create a search function that covers new'ing the array in the function.
Code:int main( ... )
{
float test_data[ ];
int test_count;
status = get_test_data( &test_data, &test_count );
for( int i = 0; i < test_count; i ++ ) { do something with the data };
... }
bool get_test_data( &test_data, &test_count )
{...
test_count = 128; // gets set from an fscanf( ... )
test_data = new float[ test_count ];
for( j = 0; j < test_count; j ++ )
fscanf( test_data_file, "%f", test_data[ j ] );
... }


Code:int main( ... )
{
float test_data[ ];
int test_count;
status = get_test_data( &test_data, &test_count );
for( int i = 0; i < test_count; i ++ ) { do something with the data };
... }
bool get_test_data( &test_data, &test_count )
{...
test_count = 128; // gets set from an fscanf( ... )
test_data = new float[ test_count ];
for( j = 0; j < test_count; j ++ )
fscanf( test_data_file, "%f", test_data[ j ] );
... }