strings.h strncmp returs always a zero

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
ondrej_bajer
Posts: 33
Joined: 17 May 2018 09:55

strings.h strncmp returs always a zero

Post by ondrej_bajer »

Hi guys,

I want to use strncmp function in order to compare start of two chararrays. But it returns me zero in every case. What am I doing wrong, please?
Test sketch here:

Code: Select all

void setup() {
   Serial.begin (9600);
}

#include <string.h>

char CharBuffer[50] = "123456789ABC";
char CompareBuffer[50] = "123456789XXX";
word NumberOfToCompare;
int Result;

void loop () {
  
  NumberOfToCompare = 5;
  Result = strncmp (CharBuffer, CompareBuffer, NumberOfToCompare);
  Serial.println(Result);

  NumberOfToCompare = 11;
  Result = strncmp (CharBuffer, CompareBuffer, NumberOfToCompare);
  Serial.println(Result);
  
  NumberOfToCompare = 12;
  Result = strncmp (CharBuffer, CompareBuffer, NumberOfToCompare);
  Serial.println(Result);
  
  delay(1000);
 }
ondrej_bajer
Posts: 33
Joined: 17 May 2018 09:55

Re: strings.h strncmp returs always a zero

Post by ondrej_bajer »

Hi guys,

just wanted to share my findings. To be honest, I found a solution after couple of hours but I was too lazy to post it here immediatelly. Shame on me.

So, the string.h library is okay, the problem is with the declaration. CharArray declared as char CharBuffer[50] = "123456789ABC" does not work correctly. However if I put chars into an array later, and then add a termination zero, it worsk like a charm.
Hope this will help somebody else.

Message to the Z-Uno development team: Thanks a lot for bringing string.h lib, it helped me a lot!

Regs,
ONDREJ
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: strings.h strncmp returs always a zero

Post by PoltoS »

Thanks, we will check what's wrong with this definition.
bastibart
Posts: 23
Joined: 12 Dec 2018 16:37

Re: strings.h strncmp returs always a zero

Post by bastibart »

Hi guys

Array initialization with string literals is still not working.

char buf[] = "hello world"; // won't fill buf[]

FIRMWARE DATA
----------------------------------------------------------
REVISION:02.14
Z-WAVE FREQUENCY:EU
ORIGINAL FW. CRC32: 45 54 B1 BC
MAXIMUM CODE SIZE: 30 KB
CURRENT FW. CRC16: FE A1
RADIO CHANNELS: 02
----------------------------------------------------------
Post Reply