Please explain code

Hi there. My environment is Ubuntu 14.04. I am trying to work with the file legendre_polynomial.c This file is here:

https://people.sc.fsu.edu/~jburkardt/c_src/legendre_polynomial/legendre_polynomial.c

I changed the extension to .cpp

There is one function inside with two "forms" for the lack of a better word:

double *pmn_polynomial_value ( int mm, int n, int m, double x[] )

and

void pmn_polynomial_values ( int *n_data, int *n, int *m, double *x, double *fx )

I presume it is overload.

They are both called in the same routine (pmn_polynomial_value_test) of file legendre_polynomial_pbr.c found at the same website

https://people.sc.fsu.edu/~jburkardt/c_src/legendre_polynomial/legendre_polynomial_prb.c

Could you explain the code? I can post more code if needed.

Thanks, - Alex
Last edited on
The gist of things is:
1
2
printf ( "                             Tabulated                 Computed\n" );
printf ( "     N     M        X       Pmn(N,M,X)                Pmn(N,M,X)                     Error\n" );

The pmn_polynomial_values() is a precalculated table of function parameters and corresponding value.

The pmn_polynomial_value() evaluates the same function.

The pmn_polynomial_value_test() shows both precalculated and computed values.

This is similar:
1
2
3
4
5
6
7
8
9
10
int foo( int y ) {
  return 2 * y;
}

void test() {
  int tab[] = {0,2,4,6};
  for ( int x = 0; x < 4; ++x ) {
    std::cout << ' ' << x << ' ' << tab[x] << ' ' << foo(x) << '\n';
  }
}



I changed the extension to .cpp

Some compilers & IDE assume that .c is C and .cpp is C++. However, you should be able to override that. But should you?

The modern C deviates from the C that compiles as C++ code. Therefore, when you try to convince your compiler that you have C++ code, you can get into unnecessary trouble. You do have a C compiler (unless you really have only C++ without c?), so let it do its work.
Keskiverto thank you. I did not notice that there is a singular and plural in the function name but could you explain the asterisk * at the function name?

*pmn_polynomial_value

Thank you.
There is no asterisk in the function name.
There is:
1
2
3
double *  // return type
pmn_polynomial_value // name
( int mm, int n, int m, double x[] ); // arguments 

C and C++ are rather liberal on the amount of whitespace
I still have trouble visualizing it. The dereference operator must indicate that a pointer is involved and the return value will be stored at a different address, correct?

Thanks.
Last edited on
What dereference operator?

The * in double* says that the type is a pointer. When a function returns a pointer, it indeed returns an address. The return value is an address.

What the caller does with the address is up to the caller.
What is at the address? Should be something valid, unless the address is nullptr.

In this case the pmn_polynomial_value() an address that it received from pm_polynomial_value() that in turn got the address from malloc().

Malloc did dynamically allocate a block of memory from heap. A block large enough to store some double values in. In other words, the pmn_polynomial_value()'s return value is the location of an array of double values.

The pmn_polynomial_value_test() stores the value (address) returned by pmn_polynomial_value() into variable fx2_vec. The type of fx2_vec is identical to the type of pmn_polynomial_value(); both are double *.

Pointers. The variable is called "pointer". The function is said to "return a pointer".


The dereferencing is then used to access a value that is stored at the address. See the second line:
1
2
    fx2_vec = pmn_polynomial_value ( 1, n, m, x_vec );
    fx2 = fx2_vec[n];


Can we presume that you are familiar with the kinship of brackets and the dereferencing *:
1
2
3
fx2_vec[ n ]
// is same as
*(fx2_vec + n)
Can we presume that you are familiar with the kinship of brackets and the dereferencing *:

Yes I am familiar with this and use it in my code all the time. Thank you for the explanation. It is very helpful.

- Alex
Topic archived. No new replies allowed.