Pointer to function

Hi there, I'm fairly new to C++ and I'm attempting to learn how to use pointers. I have the following file that creates coordinates and then moves them in random directions using a random number generator.

The value sigmaf_point is inputted from a text file:

 ``12345678910111213141516171819202122`` ``````void methane_coords(int &atom_types, double *&sigmaf_point, double *&epsilonf_point, double *&massf_point, int &atom_number, double *&x_point, double *&y_point, double *&z_point, int *&type_point) double dummy_int = 1; string dummystring; string s; ifstream Dfile; std::stringstream out; out << 1; s = out.str() + ".TXT"; Dfile.open (s.c_str()); if (Dfile.fail()) { return; } for (int i=0; i> sigmaf_point[i]; }``````

Which I then use in another function:

 ``1234567891011121314151617`` ``````double initial_energy(double **coords_fluid, double **coords_solid, int n_atoms, const double *box_size){ // Loop over all pairs of atoms and calculate the LJ energy double total_energy = 0; for (int i = 0; i <= n_atoms-1; i++) { sf1=sigmaf_point(coords_fluid[i][3]); ef1=epsilonf_point(coords_fluid[i][3]); // Energy fluid-fluid for (int j = i+1; j <= n_atoms-1; j++) {... } ``````

coords_fluid is created in the main file like so:

 ``12345`` ``````double **coords_fluid = new double*[5000]; for (int i = 0; i < n_atoms_methane; i++) { coords_fluid[i] = new double[4]; }``````

Now the problem is with `sf1=sigmaf_point(coords_fluid[i][3]);

I get the error "expression must have pointer to function type" for sigmaf_point. I'm a bit confused about this, I know it's about how I call the variable but can't seem to fix it.

Cheers
First, you should avoid using "pointer" too much!!! (especially in this situation - say honestly that's insane) In general it's extremely dangerous. I recommend you use the vectors instead.
Second :
What definition type of "sigmaf_point"? Function, variable, or anything else?
Last edited on
So what is the advantage of vectors/disadvantages of pointers. My initial impressions of them is that they are - like you say - a bit dangerous. But seeing as my C++ ability is fairly new and the programs that I need to write require me to call variables that have large amounts of data I assumed that pointers would be the best.

The problem that I've found is that when I call a function that has a loop in it and it's dealing with an array of values, if I don't use pointers then the value that gets returned is simply the value returned by the last iteration of the loop, not every iteration of the loop. Pointers seemed to fix this.

sigmaf_point is an array of 4 doubles.
Forgot to put this:

 ``12`` ``````double sigma [5]; double *sigmaf_point = sigma;``````
In the following line:

`sf1=sigmaf_point(coords_fluid[i][3]);`

you are attempting to use sigmaf_point like it's the name of a function. That's what the parentheses () mean.

However, you've obviously defined it as something else, although I can't see the definition of it in the code you've posted.

Edit: you also seem to be doing something similar in the following line:

`ef1=epsilonf_point(coords_fluid[i][3]);`

Is epsilonf_point a function?
Last edited on
Topic archived. No new replies allowed.