- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码,它从命令行获取参数并大海捞针。
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i, j, flag = 0;
for (i = 1; i < argc; i++)
{
char needle[4] = "done";
int length = strlen(argv[i]);
for (j = length - 1; j >= 0; j--)
{
// here is some simple code to print out the string
// in reverse order that I commented out.
argv[i][j] = tolower(argv[i][j]);
}
char *pch = strstr(argv[i], needle);
if (pch)
{
printf("DONE!");
}
//printf("%s", pch);
}
return 0;
}
现在,当我尝试输入以下参数时:
hi there done
我得到:
DONE!
但是如果我尝试这个:
done
我没有得到任何输出。
问题在于,如果有多个参数,它只会忽略第一个参数中出现的“done”,或者根本找不到它。另一方面,如果“done”在后面的参数中以任何形式出现,它就会找到它。为什么会发生这种情况?
最佳答案
这里有未定义的行为,因为使用非 nul 终止的字符数组调用 strstr
。
您已经像这样初始化了needle
-
char needle[4] = "done";
strstr
需要 NUL 终止的 char
数组。这里 strstr
不会知道在哪里停止比较 - 你必须为 \0
字符腾出空间。
char needle[5] = "done";
或者简单地
char needle[] = "done";
<小时/>
知道没有什么问题
char needle[4] = "done";
即使它没有存储\0
,它也没有违反任何规则。该数组用d
、o
、n
、e
初始化。但是你将它传递给了一个具有不同期望的函数 - 这就是问题所在。标准库中处理字符串的大多数函数都需要 NUL 终止的 char
数组。
来自标准§6.7.9
An array of character type may be initialized by a character stringliteral or UTF−8 string literal, optionally enclosed in braces.Successive bytes of the string literal (including the terminating nullcharacter if there is room or if the array is of unknown size)initialize the elements of the array.
关于c - strstr() - C 库函数在字符串 (haystack) 中找不到字符串 (needle),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48357603/
我的第一个堆栈溢出问题,所以我希望它格式正确。 我制作了一个模拟 strstr 的简单函数: char *my_strstr(char *haystack, char *needle) { in
假设我正在尝试在任何领域搜索“Douglas”。如果我这样做: solr/query?q=Douglas 我收到 0 条回复,但如果我这样做: solr/query?q=firstname:Dougl
我有一个字符串,看起来有点像这样 1: u:mads g:folk 2: g:andre u:jens u:joren 我需要的是一种获取例如 u:jens 和它后面的数字(1 或 2)的方法(我猜测
我通过引用以下链接中的代码在我的项目中实现了速度计。 我需要对速度计指针进行动画处理,直到从服务器获得结果,一旦获得结果,我需要根据一些计算将指针设置为正确的值。 我不明白该怎么做。 请帮我解决问题。
我写了一个小方法来查看一个字符串是否包含另一个字符串。不过我只有一个小问题,它总是返回 false。 给定大海捞针是一个名为 salaryCheck 的字符串,其值为“10.1”,针是“.”。它总是返
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有一个内置函数用于查找值的数组键 - array_search .然而,正如您从示例中看到的那样,该函数仅查找第一次出现的情况,而我需要最后一次出现: 'blue', 1 => 'red', 2 =
假设这可以正确判断子字符串是否在字符串中,是否有更简洁的方法来做到这一点? if(is_int(strpos($haystack, $needle))){ ... } 最佳答案 我不会那样做的。
我从以下代码中得到了一个奇怪的行为..它的目的是在每次按键时为用户提供自动完成选项 function autoComplete(str) { debugger; if (str
是否有一个函数可以检查数组中的某些值?我有 2 个数组,1 个带有文件扩展名,1 个带有混合内容。 In_array 仅适用于字符串。 谢谢了! 文件示例: picture21474.png 允许的扩
是否有检查数组中某些值的函数?我有 2 个数组,1 个带有文件扩展名,1 个带有混合内容。 In_array 仅处理字符串。 已经谢谢了! 文件示例: picture21474.png 允许的扩展名:
我需要一个针盘,显示实时值。该值改变针的角度,而不是位置。 到目前为止,我所能做的就是使用 ray() 或 annular_wedge() 字形,但是这个解决方案有很多问题:前面的行不是删除,几次更新
我有一张图片。在该图像上,我放置了一个针图像,我想将其旋转一定 Angular 。一切都运行良好。我想要的是改变针的 0 度位置。目前它在顶部,我想在底部制作 0 度点。请参阅下面的附图。 这是我为此
我想在流模式下使用 node.js 的 needle 模块,类似于 needle 文档中的这个例子: var stream = needle.get('http://www.as35662.net/1
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我有以下代码,它从命令行获取参数并大海捞针。 #include #include int main(int argc, char **argv) { int i, j, flag = 0;
我想将一个小图像从一台服务器移动到另一台服务器(都运行 Node )。当我搜索时,我还没有找到足够的东西。 This该帖子仍未得到答复。 当我开始试验时,我向第一台服务器写入了以下内容: app.po
请求模块有参数localAddress。 options = { url: "https://ru.tradeskinsfast.com/ajax/botsinventory",
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
下面的代码采用一个关键字和一串文本(经过 html 标记清理),并确定该关键字是否出现在经过清理的内容的最后一句话中。 有一个小故障我想不通。当内容末尾包含一个空格或带有不间断空格的段落标签时,即 T
我是一名优秀的程序员,十分优秀!