compile error

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Apr 2006
Posts: 15
Reputation: puppy is an unknown quantity at this point 
Solved Threads: 0
puppy puppy is offline Offline
Newbie Poster

compile error

 
0
  #1
May 9th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,757
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: compile error

 
0
  #2
May 9th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

Re: compile error

 
1
  #3
May 12th, 2006
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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum


Views: 1115 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC