c中的字符串比较

Oma*_*ban 2 c string char microblaze

我现在正在为fpga上的microblaze编写交流程序,我现在想检查一下我是否收到了消息,但是strncmp和strcmp不工作的唯一方法就是这样:

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

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

但是,一旦我处理较长的文本,这将很难,所以任何好方法?我用这种格式试了strncmp:

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

Vic*_*and 5

来自http://www.cplusplus.com/reference/cstring/strncmp/:

int strncmp(const char * str1, const char * str2, size_t num);
Run Code Online (Sandbox Code Playgroud)

您是否忘记提供num,要比较的最大字符数?

strncmp函数使用它,但strcmp没有!如果比较整个字符串,后者可能就是你想要的.