Question

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 
Was it helpful?

Solution 2

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.

OTHER TIPS

From http://www.cplusplus.com/reference/cstring/strncmp/:

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

Did you perhaps forget to supply num, the maximum number of characters to compare?

The function strncmp uses it, but strcmp does not! If comparing whole strings, the latter one is probably what you want.

Try to recompile your program with the warnings (-Wall -Wextra).

My guess is that you forgot to include the definition of the strncmp at the begining of the source file like this:

#include <string.h>

So, when the warnings will be activated you should see appear the following message:

warning: implicit declaration of function 'strncmp()'

Try to always activate the warnings when compiling, it's very helpful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top