1

I'm wondering how to properly use pointers in for and while loops in C++. Usually I write using C instead of C++. The only reason I'm using the C++ std library this time is so I can use the complex number functions required by other mathematical functions in the code.

As part of the assignment we were given the following function declaration. The part that I wrote is commented within the function.

 typedef std::complex<double> complex;

 // Evaluates a polynomial using Horner's approach.
 // Inputs:
 //  [coeffs, coeffs_end) - polynomial coefficients, ordered by descending power
 //  x - point of evaluation
 // Outputs:
 //  p - value of polynomial at x
 //  dp - value of polynomial derivative at x
 //  ddp - value of polynomials second derivative at x
 //
 template<typename T>
 inline void poly_val(T const* coeffs, T const* coeffs_end, T x, T & p, T & dp, T & ddp)
  {
      //MY CODE HERE
      int i = 0;
      const T *pnt = coeffs;
      while(pnt != coeffs_end){
                  //Evaluate coefficients for descending powers
          p += coeffs(i)*pow(x,((coeffs_end-1)-i));
          pnt++;
          i++;
      }
 }

The function doesn't know the length of the array, so I'm guessing the stop condition is the pointer 'coeffs_end', which points to the last value in the array 'coeffs'. Can I use a pointer in a conditional this way? (traditionally I would have fed the length of the array into the function, but we cant modify the declarations)

If I do it this way I keep get an error when compiling (which I don't get):

C2064:term foes not evaluate to a function taking 1 arguments

for the following line:

      p += coeffs(i)*pow(x,((coeffs_end-1)-i));
3
  • 1
    What exactly do you intend for coeffs(i) to do? coeffs isn't a function, is it? Commented Oct 10, 2011 at 0:04
  • 2
    Are you sure it's foes and not coeffs in the error? Also, should it be coeffs[i] rather than coeffs(i)? Commented Oct 10, 2011 at 0:04
  • yeah its meant to be coeffs[i]. Sorry been using matlab too much... Commented Oct 10, 2011 at 0:05

2 Answers 2

2

coeffs(i) is calling convention to a function that takes an integer argument. But in your case it is an pointer. So, you need to use [] operator to access the element at it's index.

Also ((coeffs_end-1)-i) resolves to an address location. You need to dereference it to get the value at the location.

Sign up to request clarification or add additional context in comments.

3 Comments

Awesome. I changed that now I'm getting the compile error error C2665: 'std::pow' : none of the 8 overloads could convert all the argument types
@user986875 - As I said, you need to *((coeffs_end-1)-i) dereference the location. Notice the * operator before.
@user986875 Make sure that the pointer arithmetic resolves to a valid memory location to dereference. Always play safe with pointer arithmetic. Good luck.
1

Maybe it'd be more readable to write this in a cleaner fashion:

#include <cmath>
#include <iterator>

template<typename T>
inline void poly_val(T const* coeffs, T const* coeffs_end, T x, T & p, T & dp, T & ddp)
{
  const std::size_t nterms = std::distance(coeffs, coeffs_end);
  for (std::size_t i = 0; i != nterms; ++i)
  {
    p += coeffs[i] * std::pow(x, nterms - 1 - i);
  }
}

Since raw pointers can be treated as iterators, we can use std::distance to determine the size of an array bounded by a range [first, last).


Edit: Acutally it can be done even easier:

  for (const T * it = coeffs; it != coeffs_end; ++it)
  {
    p += *it * std::pow(x, std::distance(it, coeffs_end) - 1);
  }

4 Comments

Nice!!! That's extremely useful. Didn't know about std::distance. Thanks for the tip.
@user986875: distance not strictly necessary, since you could have said coeffs_end - coeffs, but it's a nice touch since it's very self-descriptive.
@user986875: I added another, even simpler alternative.
@ Kerrek SB Woot! Even tidier! Makes me wonder why they don't start us on C++ to begin given the vast number of useful functions available. But I guess its a 'start from the basics' mentality ^.^

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.