cancel
Showing results for 
Search instead for 
Did you mean: 

compare two char arrays

Ala
Senior

Hi there

I am trying to compare two char arrays. one is

char CODENUMBER[36]="06A9062F0020063406450627063106470200";

and the other one is a 250 element array called

char Rcv[250]

in my code, I receive messages and I fill Rcv[]'s elements with those messages. I want to check if I could get that CODENUMBER[] array in Rcv[]. so I wrote

int i=0,c=0;
for(int j=0;j<250;j++){                                                   //Check if CODENUMBER is sent correctly
 
while(strcmp(Rcv[j],CODENUMBER[i])==0 && i<36){
i++;
c++;	
if(c==36){
//do something
i=0;
c=0;
}
}
}

but I get this error

INCOMPATIBLE INTEGER TO POINTER CONVERSION PASSING 'CHAR' TO PARAMETER OF TYPE 'CONST CHAR *';TAKE THE ADDRESS WITH &

why do I get this message?

6 REPLIES 6
TDK
Guru

strcmp expects a null-terminated string as an argument. You're passing a char. Just compare the characters directly.

while (Rcv[j] == CODENUMBER[i] && i < 36) {

There are other logic errors in your code. If you want to see if the first 36 characters are the same:

int i = 0;
while (i < 36 && Rcv[i] == CODENUMBER[i]) {
  i++;
}
if (i == 36) {
  // they match
} else {
  // they don't match
}

If you feel a post has answered your question, please click "Accept as Solution".

>>why do I get this message?

Because what you're doing is wrong. As best I can see the syntax and logic is very confused.

I'm not even clear what it is you're trying to achieve. Perhaps provide some example patterns of what you think match, vs doesn't

strstr() would find a string within a string

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi dear TDK

thanks for your reply. but I didn't get "null-terminated string"? can you explain it with an example?

the other thing is that Rcv[] is a 250 element array and I'm not sure where is this matching between Rcv[] and CODENUMBER[] located. that's why I can not say if this matching is my first 36 characters of Rcv[] or else...

dear clive1

as always you're a big help and you always reply very helpfully:ok_hand:

why is this wrong? can you explain what do you mean by "syntax and logic"?

all I want to do is to check if I can get the exact match of CODENUMBER[]'s elements in Rcv[]'s elements in the exact same order that CODENUMBER[] elements are...

can you help me with a clear solution?

If the string isn't properly terminated​ the string functions are apt to crash.

An array of characters or an array of strings​?

&Rcv[j] would be the address (pointer) to the j'th element.

Currently you're passing individual characters to functions expecting string pointers.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

> but I didn't get "null-terminated string"?

https://en.wikipedia.org/wiki/Null-terminated_string

If you feel a post has answered your question, please click "Accept as Solution".