我正在为我的microblaze在fpga上写一个c程序,现在我想检查我是否收到了消息ok,但strncmp和strcmp不工作,唯一有效的方法是:
char*as=malloc(sizeof(int));
as=p->payload;
if (*(as)=='o') {//first letter o
if (*(as+1)=='k') {//second letter 但是一旦我处理更长的文本,这将是困难的,所以有什么好的方法吗?我尝试了以下格式的strncmp:
if (strncmp(as,"ok",2)==0) //didnt work even changing 0 to 1 it just doesnt detectct it 发布于 2013-04-03 03:36:38
检查"strncmp“的语法
int strncmp ( const char * str1, const char * str2, size_t num );其中,str1是要比较的C字符串,str2是要比较的C字符串,num是要比较的最大字符数。
我认为引入第三个变量num,即你想要比较的最大字符数将会解决你的问题。
发布于 2013-04-03 03:26:15
来自http://www.cplusplus.com/reference/cstring/strncmp/
int strncmp(const char * str1, const char * str2, size_t num);您是否忘记了提供num,即要比较的最大字符数?
函数strncmp使用它,但strcmp不使用它!如果比较整个字符串,后一个字符串可能就是您想要的。
发布于 2013-04-03 04:57:05
尝试重新编译带有警告(-Wall -Wextra)的程序。
我的猜测是,您忘记在源文件的开头包含strncmp的定义,如下所示:
#include <string.h>因此,当警告将被激活时,您应该看到出现以下消息:
warning: implicit declaration of function 'strncmp()'尝试在编译时始终激活警告,这非常有用。
https://stackoverflow.com/questions/15772584
复制相似问题