gpt4 book ai didi

c - c中的字符串比较

转载 作者:行者123 更新时间:2023-12-02 06:37:11 24 4
gpt4 key购买 nike

我正在 fpga 上为我的 microblaze 编写一个 c 程序,现在我想检查我是否收到了消息,但是 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 

最佳答案

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

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

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

strncmp函数使用了它,但是strcmp没有!如果比较整个字符串,后一个可能是您想要的。

关于c - c中的字符串比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15772584/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com