Using argv in C?

自作多情 提交于 2019-11-27 16:09:44

The line

if(argv[1]=="-e"){

compares pointers, not strings. Use the strcmp function instead:

if(strcmp(argv[1],"-e")==0){

Change:

if(argv[1]=="-e"){

to

if(strcmp(argv[1], "-e") == 0){

and include string.h.

Check out getopt() and related functions; it'll make your life a lot easier.

You can't compare c-strings like that. Use strcmp (reference here).

Because c-strings are actually pointers, the == operator compares the address of the first character which will never be equal in this case.

You can't use == to compare strings like that in C. That's just comparing the addresses of argv[1] and your literal, which are pretty much guaranteed to be different.

Use strcmp instead. eg:

if (!strcmp("-e", argv[1])) {

The prototype of the main function says you're dealing with char* pointers. In C, there is no operator overloading; therefore, == between two char* will test if they point to the same place. This is not the case, and is rarely the case at all. Use the strcmp (the reference for the function is valid even though it points to a C++ site) function from <string.h>:

strcmp(argv[1], "-e") == 0
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!