简体   繁体   中英

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. 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. Thus, it's not a valid string so you can't use strcmp() on it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM