View previous topic :: View next topic |
Author |
Message |
FFT
Joined: 07 Jul 2010 Posts: 92
|
strcmp strange length limitation [Solved] |
Posted: Sun Jan 09, 2022 11:52 am |
|
|
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
|
|
Posted: Sun Jan 09, 2022 12:29 pm |
|
|
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
|
|
Posted: Sun Jan 09, 2022 2:15 pm |
|
|
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
|
|
Posted: Mon Jan 10, 2022 1:25 am |
|
|
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. |
|
|
|