[英]Comparing char* in C
I am trying to compare patterns of bits, to do this I have a struct which holds a pattern as a string. 我试图比较位模式,为此我有一个结构,将模式保存为字符串。 I then want compare that string to another string I built.
然后我想将该字符串与我构建的另一个字符串进行比较 I am using strcmp, however it is matching two strings even though they do not match.
我正在使用strcmp,但它匹配两个字符串,即使它们不匹配。 This is my code
这是我的代码
typedef struct
{
int emp;
char *context;
int numOnes;
int numZeros;
}Pattern;
char *patt, str[t+1];
patt = str;
while(count<t){
//printf("Stream: %d\n", stream[end]);
if(stream[end] == 1){
patt[count]= '1';
//writeBit(1);
}
else{
patt[count]='0';
//writeBit(0);
}
end--;
count++;
} //code to build a string
while(i<(1<<t)&&(found==0)){
if((strcmp(patt,patterns[i].context)==0)){
if(patterns[i].numOnes <= patterns[i].numZeros){
prediction = 0;
checkPredict(prediction,stream[end], i);
}
else{
prediction = 1;
checkPredict(prediction,stream[end], i);
}
found = 1;
}
else{
found = 0;
}
i++;
}//comparing string
You never terminate patt
, ie write the '\\0'
character after the last character. 你永远不会终止
patt
,即在最后一个字符后写'\\0'
字符。 Thus, it's not a valid string so you can't use strcmp()
on it. 因此,它不是一个有效的字符串,所以你不能在它上面使用
strcmp()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.