[英]linux command line: passing wrong argument?
I'm debugging my C++ code on Ubuntu which I execute as ./main path < input.txt
我在Ubuntu上调试我的C ++代码,我执行
./main path < input.txt
argv[1]
should contain a string "path" which verified by GDB below. argv[1]
应该包含一个字符串“path”,由下面的GDB验证。
However, the program always jumps over my first condition even though argv[1] == "path"
should return true. 但是,即使
argv[1] == "path"
应该返回true,程序也会跳过我的第一个条件。
Any idea? 任何想法?
...
(gdb) n
181 if(argv[1] == "path")
(gdb) p argv[1]
$1 = 0xbffffba3 "path"
(gdb) n
183 else if(argc == 1)
(gdb)
Even though the two strings are the same, they occupy a different location in memory. 即使两个字符串相同,它们在内存中占据不同的位置。 When it is checking whether
argv[1]
is equal to a fixed-string "path"
, it is not doing it character-by-character: it is looking to see whether the pointer to the fixed-string "path"
is the same as the argv[1]
variable passed into main()
, which it is not. 当检查
argv[1]
是否等于固定字符串"path"
,它不是逐个字符地执行它:它正在查看指向固定字符串"path"
的指针是否相同作为argv[1]
变量传递给main()
,它不是。
You need to use strcmp to test the equality of those two strings: 您需要使用strcmp来测试这两个字符串的相等性:
if (strcmp(argv[1], "path") == 0) { /* they match */ }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.