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

strcmp strange length limitation [Solved]

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



Joined: 07 Jul 2010
Posts: 92

View user's profile Send private message

strcmp strange length limitation [Solved]
PostPosted: Sun Jan 09, 2022 11:52 am     Reply with quote

Hello,

The following statement is true TEST_VAL's char length is 4 or less
Code:
if(strcmp((char *)TEST_VAL, (char *)&Buffer[1]) == 0)

true when:
Code:
#define TEST_VAL    "1234"

false when:
Code:
#define TEST_VAL    "123412341234123412341234"

Obviously Buffer[1] is start of the same string compared with the TEST_VAL.

I also tried using
Code:
#device PASS_STRINGS = IN_RAM

but nothing changed.

Buffer is defined as:
Code:
unsigned int8 Buffer[64];

and filled by serial communication.

I also tried with a custom implementation of the strcmp, result is the same.

Is this something special to CCS?

Thanks


Last edited by FFT on Mon Jan 10, 2022 8:25 am; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jan 09, 2022 12:29 pm     Reply with quote

This works:
Code:
#include <18F46K22.h>
#fuses NOWDT 
#use delay(internal=4M)
#use rs232(UART1, baud=9600, ERRORS)

#include <string.h>

unsigned int8 Test_val[64] =  "123412341234123412341234";   

unsigned int8 Buffer[64] =  "123412341234123412341234";   

//=================================
void main()
{

if(strcmp(test_val, Buffer) == 0)   
   printf("Matched\r");
else
   printf("No match\r");
   
   
while(TRUE);
     
}

I see two problems in your posted code.

Below, you are trying to create a pointer to a constant.
The compiler will give you an error. It will not compile.
Quote:
if(strcmp((char *)TEST_VAL, (char *)&Buffer[1]) == 0)


Below, you apparently think a string starts at index 1.
But it actually starts at index 0.
Quote:
if(strcmp((char *)TEST_VAL, (char *)&Buffer[1]) == 0)
FFT



Joined: 07 Jul 2010
Posts: 92

View user's profile Send private message

PostPosted: Sun Jan 09, 2022 2:15 pm     Reply with quote

PCM programmer wrote:

Below, you are trying to create a pointer to a constant.
The compiler will give you an error. It will not compile.
Quote:
if(strcmp((char *)TEST_VAL, (char *)&Buffer[1]) == 0)



Thanks! This fixed my issue, v5.104 did not give an error for this. I just used a RAM variable and issue has fixed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Jan 10, 2022 1:25 am     Reply with quote

You can use the #defined version with this:
Code:

#include <18F46K22.h>
#device PASS_STRINGS=IN_RAM
#fuses NOWDT
#use delay(internal=4M)
#use rs232(UART1, baud=9600, ERRORS)
#define Test_val  "123412341234123412341234" 

#include <string.h>

unsigned int8 Buffer[64] =  "123412341234123412341234";   

//=================================
void main()
{
   if(strcmp(test_val, Buffer) == 0)   
      printf("Matched\r");
   else
      printf("No match\r");
   while(TRUE); 
}


The specific things. First understand what PCM has told you about where strings 'start'. The first byte is [0], not [1]. Then note the second line
in the code I posted. This tells the compiler to 'virtualise' constant strings
into RAM when needed.
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