Search code examples
cstringcharmicroblaze

string comparison in c


I am writing a c program for my microblaze on the fpga now i want to check if i recieved the message ok but strncmp and strcmp are not working the only way that is working is this way :

           char*as=malloc(sizeof(int));

    as=p->payload;
            if (*(as)=='o') {//first letter o
    if (*(as+1)=='k') {//second letter 

but this will be hard once i deal with longer text , so any good approach ? i tried strncmp in this format :

         if (strncmp(as,"ok",2)==0)      //didnt work even changing 0 to 1 it just doesnt detectct it 

Solution

  • check the syntax of "strncmp"

    int strncmp ( const char * str1, const char * str2, size_t num );
    

    where str1 is the C string to be compared, str2 is the C string to be compared and num is the maximum number of characters to compare.

    I think introducing the third variable num i.e the maximum no of character you want to compare will solve your problem.