943,521 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1254
  • C RSS
May 9th, 2006
0

compile error

Expand Post »
Hi,
Can someone tell me how to fix the function below. I am getting this compile error regarding the last line:
cannot convert `const double' to `const double*' in return
What should I return according to the function parameters I've written?

Thanks.

  1. const double * maximum(const double a[], int size)
  2. {
  3. if (size == 0) return NULL;
  4. const double *highest = a;
  5. for (int i = 0; i < size; i++)
  6. if (a[i] > *highest)
  7. {
  8. *highest == a[i];
  9. }
  10. return *highest;
Last edited by Dave Sinkula; May 9th, 2006 at 10:11 pm.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Newbie Poster
puppy is offline Offline
15 posts
since Apr 2006
May 9th, 2006
0

Re: compile error

The syntax error being reported means you have to return highest, not the highest derefenced.

The logic error is that you need to have the ability to change the value of highest so don't try to force highest to be a const double in the first place.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
May 12th, 2006
1

Re: compile error

hi
There are two things first
  1. const double *highest = a;
this line says highest is a pointer to a costant means u cant change the content highest is pointing to, next
  1. if (a[i] > *highest)
  2. {
  3. *highest == a[i]; // "==" ??
  4. }
removing that if u do it like
  1. if (a[i] > *highest)
  2. {
  3. *highest = a[i];
  4. }
u will be trying to change a contant, which is illegal.

better if u write
  1. const double * maximum(const double a[], int size)
  2. {
  3. if (size == 0) return NULL;
  4. double *highest = (double*)a;
  5. for (int i = 0; i < size; i++)
  6. if (a[i] > *highest)
  7. {
  8. *highest = a[i];
  9. }
  10. return highest;
  11. }
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Translating from Matlab to C
Next Thread in C Forum Timeline: 1-D array





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC