CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Issues setting up bsearch - Invalid identifier

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kylemorin



Joined: 11 Mar 2007
Posts: 4

View user's profile Send private message AIM Address

Issues setting up bsearch - Invalid identifier
PostPosted: Mon Mar 12, 2007 11:41 am     Reply with quote

Hello all. I have been trying to get bsearch to work for quite some time now to no avail. I keep getting the error message "Expecting an identifier," even when I use the exact sample code in the CCS help file. There are no example programs in the CCS example directory that use it either. I've searched google for bsearch code too, and I came up with what's below, but it still gives me the same error. This tells me I must be setting something up horribly wrong, or I must be missing something big. Any insight is appreciated!

Code:
#INCLUDE <12F683.h>           
#USE delay(clock=4000000) 
#FUSES nomclr, nowdt

#INCLUDE <stdlib.h>
int compareints (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int values[] = { 10, 20, 25, 40, 90, 100 };

int1 main ()
{
  int * pItem;
  int key = 40;
  pItem = (int*) bsearch (&key, values, 6, sizeof (int), compareints);
  if (pItem!=NULL)
    return 1;
  else
    return 0;
  return 0;
}




Something of note is that I still get the "expecting an identifier" message when I just have the compareints function below there with no code in the main loop.

Code:
int compareints (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}


Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 12, 2007 1:24 pm     Reply with quote

I made some changes and got it to compile. I used PCM vs. 4.027.
I didn't test it. I'll leave that to you.

Also, be aware that in CCS, there is nothing for main() to return to.
You're trying to return to some O/S that doesn't exist. You need
to put a while(1) statement at the end of main(). If you don't do
this, the PIC will execute a hidden SLEEP instruction that is inserted
at that location by the compiler. The PIC will go to sleep.
Also, since main() doesn't return, you should declare it as 'void',
not as 'int1'.

A few other comments. The #use delay() statement should go
below the #fuses statement. You're also missing an oscillator
parameter in your #fuses statement (XT, HS, etc.)

Comments on the CCS example code in the manual and help file:
The bsearch entry has been in there since late version 3 of the
compiler. It shows function parameters that are pointers to
constants. That wasn't supported in vs. 3 and I don't think its yet
supported in vs. 4. I think a consultant was hired to write up
some of these Unix type functions, and the Unix syntax was just
dropped into the CCS manual without testing it. That's why
you're getting an error. You can see that I've fixed that problem
in the code below. I've removed the 'const' declaration.
Also, the compare function needs to return a 'signed int'. I've
fixed that as well.

Again, this code is not tested. It's merely able to compile.
It's up to you to test it and take it further if you wish.

Code:

#include <16F877.h>
#fuses XT,NOWDT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <stdlib.h>

signed int compareints (int *a, int *b)
{
 return (*a - *b);
}

//=================================
void main()
{
int key;
int nums[5]={1,2,3,4,5};
int *ip;
_Cmpfun compare;

compare = compareints;

ip = bsearch(&key, nums, sizeof(nums), sizeof(int), compare);
     

while(1);
}
kylemorin



Joined: 11 Mar 2007
Posts: 4

View user's profile Send private message AIM Address

PostPosted: Mon Mar 12, 2007 2:05 pm     Reply with quote

PCM, I've modified your code a little bit to test it out and everything works great! I've posted below a fully functional example of how to use bsearch, so future forum browsers looking for bsearch syntax can have something to lean on so to speak. In my original code where I defined int1 main(), I actually meant to define a separate function and write the main function and while(1) loop below it. Thanks for your insight PCM!


Functional example code demonstrating bsearch usage:

Code:
#include <12F683.h>
#fuses XT,NOWDT,BROWNOUT,PUT
#use delay(clock=4000000)

#include <stdlib.h>

signed int compareints (int *a, int *b)
{
   return (*a - *b);
}

//=================================
void main()
{
   int key = 4;
   int nums[5]={1,2,3,4,5};
   int *ip;
   _Cmpfun compare;
   compare = compareints;

   ip = bsearch(&key, nums, sizeof(nums), sizeof(int), compare);
   if (ip != NULL)
      output_high(PIN_A2);
   else
      output_low(PIN_A2);
   while(TRUE);
}
kylemorin



Joined: 11 Mar 2007
Posts: 4

View user's profile Send private message AIM Address

PostPosted: Tue Mar 13, 2007 11:27 am     Reply with quote

Since bsearch and qsort use similar syntax and often go hand-in-hand, and since the example in the CCS help file is not yet functional, I've posted functional code below as a follow-up demonstrating implementation of qsort. Hopefully someone will find it useful someday =o)

Code:
#include <12F683.h>
#fuses XT,NOWDT,BROWNOUT,PUT
#use delay(clock=4000000)

#include <stdlib.h>

signed int sortcompare(int *arg1,int *arg2)  {
   if ( *arg1 < *arg2 )
      return -1;
   else if ( *arg1 == *arg2)
      return 0;
   else return 1;
}   

void main()  {
   int nums[5]={ 2,3,1,5,4};
   _Cmpfun compare;
   compare = sortcompare;
   qsort ( nums, 5, sizeof(int), compare);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group