[英]Comparing char* in C
我試圖比較位模式,為此我有一個結構,將模式保存為字符串。 然后我想將該字符串與我構建的另一個字符串進行比較 我正在使用strcmp,但它匹配兩個字符串,即使它們不匹配。 這是我的代碼
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
你永遠不會終止patt
,即在最后一個字符后寫'\\0'
字符。 因此,它不是一個有效的字符串,所以你不能在它上面使用strcmp()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.