简体   繁体   中英

Trouble passing a pointer array to a function

In a program I am currently working on I have a template function included in a separate .h file that reads in five columns of data from a .txt file. The data is passed to the main program and in this instance I only care about the array title "MISC_DATA". I am trying to determine the largest value in the array "MISC_DATA" and have written another function that the data has to be passed to, in order to determine this. However, the compiler is telling me that it does not recognize the function call "Maximum_Value". I am pretty sure that it is having problems with the variable MISC_DATA included in the routine call and not the function itself. Either it does not recognize MISC_DATA as an array or I have the syntax wrong. I'm only including the important snippets of code to make it more readable. The Read_Five_Columns functions works fine, it is the function "Maximum_Value", which is not being recognized by the compiler because of how the pointer array MISC_DATA is written in the main program. For clarification the variable MISC_DATA in the function call is a float which contains the array and the variable "size_Mis" is an integer which contains the array size. Any thoughts would be appreciated.

int main(int argc, const char * argv[]) {

#include "Use_RNG.h"
#include "Read_Columnar_File.h"
#include <fstream>
#include <iostream>

 std::vector<std::string> str3;
 std::vector<int> str4;
 std::vector<char> str5;
 std::vector<int> str6;

    unsigned long size_Mis;
    std::vector<float> MISC_DATA;  // Reads in Misc. spending data
    char File1[8];
    strcpy(File1, "Misc.txt");
    Read_Five_Columns(File1,MISC_DATA,str3,str4,str5,str6);
    str3.clear(); str4.clear(); str5.clear(); str6.clear();
    size_Mis = MISC_DATA.size();

float value;
value = Maximum_Value(MISC_DATA,size_Mis);

end_time = clock();
std::cout << std::endl << "Total Time: " << (end_time-start_time)/CLOCKS_PER_SEC << std::endl;
return 0;
}

int Maximum_Value(float *array,int array_size)
{
    float max = 0;
   for(int i =10; i < array_size-1; i++)
   {
        if(array[i] > max) max = array[i];
   }
    return max;
}

There are four problems I see here.

int main(int argc, const char * argv[]) {

#include "Use_RNG.h"
#include "Read_Columnar_File.h"
#include <fstream>
#include <iostream>

All of this stuff is in the wrong order. You should not include system header files into function bodies, and typically you include standard library stuff before other stuff. Fix it to read like this:

#include <fstream>
#include <iostream>

#include "Use_RNG.h"
#include "Read_Columnar_File.h"

int main(int argc, const char * argv[]) {

Secondly, you don't declare Maximum_Value before you use it. You need to either move the definition of this function before the definition of main() or you need to add a prototype before main() :

int Maximum_Value(float *array,int array_size);

int main(int argc, const char * argv[]) {

Then, you attempt to pass an std::vector<float> as a float* which does not work:

value = Maximum_Value(MISC_DATA,size_Mis);

However, because the storage for vectors is guaranteed to be contiguous and laid out like an array, you can pass a pointer to the first member safely:

value = Maximum_Value(&MISC_DATA[0],size_Mis);

Finally, you return int from Maximum_Value when you should probably be returning float .


If possible I would suggest leveraging std::max_element , which is part of the standard <algorithm> header:

// If you don't have C++11 then use std::vector<float>::iterator instead of auto.
auto max = std::max_element(MISC_DATA.begin(), MISC_DATA.end());

Now max is an iterator to the largest element, so *max would be the largest float itself.

(If the input range was empty, then max will be equal to MISC_DATA.end() , so the equivalent to your function would be value = max == MISC_DATA.end() ? 0f : *max; .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM