Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

C18 how to manage pointers

Status
Not open for further replies.

cherreram

Newbie level 5
Joined
Mar 5, 2008
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,355
Hi,
due to my work, recently I moved from CCS to C18 and everything ok, except for one thing: I dont know how to manage pointers in C18.

Time ago I built a string search routine who worked similar to "find" python instruction.
So, I wanted to use it now, in C18, but I have -- Error [1131] type mismatch in assignment-- in return.

this is the code:

Code:
signed short int finds(char *string1, char *string2){
//Funcion para buscar string2 en string1. La max. longitud de string1 y string2
// es 128 caracteres.Devuelve la posicion del inicio de string2 en string1.
//Si no lo encuentra, devuelve un -1.
//
   unsigned int counter1=0;
   unsigned int counter2=0;
   unsigned int startostring2=0;
   unsigned int startostring1=0;
   startostring2=string2;  //this is to hold the start address of string2 and str1 
   startostring1=string1;  //and here is where error [1131] type begin.
   while(*string1!=0){
      counter2=counter2+1;
      string1=string1+1;
   }
   string1=startostring1;
   while(counter1!=counter2){
      if(*string2!=*string1){
         string1=string1+1;
         counter1=counter1+1;
      }
      else{
         do{
            string1=string1+1;
            string2=string2+1;
            }while((*string2==*string1)&&(*string2!=0));
         if(*string2==0){
            return counter1;
         }
         else{
            counter1=string1-startostring1;
            string2=startostring2;
         }
      }
   }
   return -1;
}

this routine search for string2 into string1 and return the position, on string1, where string2 start to match. If string2 is not contained in string1, -1 is returned.

this is the way I use it:

Code:
signed short res=0;
char Buffer[]="this is a test only";
res=finds(&Buffer,"test");

in MikroC or CCS it works perfect, but C18 dont.

any advice?..I know there are ram and rom pointers, but when I have to use it?
 

Could you elaborate a bit on the trouble you're having?

Is there some reason you're not treating strings as char*'s or char[]'s?

I've used C18 for all of my PIC18 development and haven't had trouble with pointers, so a bit more information would be very helpful to understand the problem.
 

Hi again,

Thanks for your cooperation and problem solved.
In C18 is not possible to assign pointer's value to a variable. In the code above I had:

Code:
startostring2=string2; -->Error

startostring is a ram variable who retain the address contained in string2 (a pointer).
Even startostring is a 16 bit value and it is able to manage the 16 bit value stored in string2, there was an Error [1131] type mismatch in assignment.

The problem is, startostring2 is a variable and string2 is a pointer.
what to do?, simple, let's startostring be a pointer.

Code:
char *startostring2;

startostring is a pointer now and, it has to be declared exactly as string was. I mean, if string2 is a char, startostring2 has to be a char. If string2 is a unsigned char, startostring2 has to be a unsigned char and so on.

If we declare string2 as a char and startofstring2 as a unsigned char, we will get the same error code: mismatch in assignment.

Well, my code, now turns into:

Code:
signed short int finds(char *string1, char *string2){
   unsigned int counter1=0;
   unsigned int counter2=0;
   char *startostring2;
   char *startostring1;
   startostring2=string2;   
   startostring1=string1; 
   while(*string1!=0){
      counter2=counter2+1;
      string1=string1+1;
   }
   string1=startostring1;
   while(counter1!=counter2){
      if(*string2!=*string1){
         string1=string1+1;
         counter1=counter1+1;
      }
      else{
         do{
            string1=string1+1;
            string2=string2+1;
            }while((*string2==*string1)&&(*string2!=0));
         if(*string2==0){
            return counter1;
         }
         else{
            counter1=string1-startostring1;
            string2=startostring2;
         }
      }
   }
   return -1;
}

well, now works fine for me. :)

Hope it can help somebody.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top