简体   繁体   English

C将const char *转换为char

[英]C convert const char * to char

I searched quite a while to find the answer, but I could only find a solution for C++ that didn't seem to work for C . 我搜索好半天才找到了答案,但我只找到一个解决方案C++ ,这似乎不是为工作C I'm trying to convert argument of const char * to char to use in my switch statement. 我正在尝试将const char *参数转换为char以在我的switch语句中使用。 I tried various things like strdup() , but was unsuccessful. 我尝试了各种各样的东西,比如strdup() ,但没有成功。

#include <stdio.h>

int main(int argc, const char *argv[]) {

    char argOne = argv[1];
    char argTwo = argv[2];

    switch(argOne) {
        case '1234' :
            printf("1234!\n" );
            break;
        default :
        printf("Invalid\n" );
    }
}

While compilation: 编译时:

warning: incompatible pointer to integer conversion initializing 'char' with an expression of type 'const char *' [-Wint-conversion] 警告:指向整数转换的不兼容指针使用类型为'const char *'的表达式初始化'char'[-Wint-conversion]

 char argOne = argv[1]; ^ ~~~~~~~ 

warning: overflow converting case value to switch condition type (825373492 to 52) [-Wswitch] 警告:溢出转换案例值到切换条件类型(825373492到52)[-Wswitch]

 case '1234' : ^ 

In your code, 在你的代码中,

  • Point 1: 第1点:

      char argOne = argv[1]; 

    is very wrong. 是非常错误的。 You cannot put a const char * (pointer) to a char variable. 您不能将const char * (指针)放入char变量。

  • Point 2: 第2点:

      case '1234' 

    also wrong. 也错了。 Here, the '1234' does not denote a string. 这里, '1234' 表示字符串。 It is a multibyte charcater, which is most probably something you don't want. 它是一个多字节的charcater,很可能是你不想要的东西。 Again, even you change that to something like "1234" , still it would be incorrect , as it will not give you the intended value, and strings cannot be used for case statement values . 同样,即使你将其改为类似"1234"东西,它仍然是不正确的 ,因为它不会给你预期的值,并且字符串不能用于case语句

Solution: Instead of using the switch case here, you can try using strcmp() to compare the incoming string and chose accordingly. 解决方案:您可以尝试使用strcmp()来比较传入的字符串并相应地选择,而不是在此处使用switch case。


Note: The recommended signature of main() is int main(int argc, char *argv[]) 注意: main()的推荐签名是int main(int argc, char *argv[])

You're getting mixed up between char (character) and char * (string). 你在char (字符)和char * (字符串)之间混淆了。 Also you can not use strings as switch/case labels. 此外,您不能使用字符串作为开关/案例标签。 Here is a fixed version of your code: 以下是您的代码的固定版本:

#include <stdio.h>

int main(int argc, const char *argv[]) {

    const char *argOne = argv[1];
    const char *argTwo = argv[2];

    if (strcmp(argOne, "1234) == 0)
    {
        printf("1234!\n");
    }
    else
    {
        printf("Invalid\n");
    }        
    return 0;
}

First of all the standard declaration of main looks like 首先是主要的标准声明看起来像

int main(int argc, char *argv[])
                   ^^^^^^

That is the second parameter does not have qualifier const . 那是第二个参数没有限定符const

Secondly argv[1] has type char * . 其次argv [1]的类型为char * There is no any sense to compare it with a character literal similar to '1234' . 将它与类似于'1234'字符文字进行比较是没有任何意义'1234' As for string literal "1234" when it may not be used in the case label. 至于字符串文字"1234" ,它可能不在案例标签中使用。

What you want is the following 你想要的是以下内容

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {

    if ( argc > 2 )
    { 
        char *argOne = argv[1];
        char *argTwo = argv[2];

        if ( strcmp( argOne, "1234" ) == 0 )
        {
            puts( "1234!" );
        }
        else
        {
            puts( "Invalid" );
        }
    }
}

You can't (really) "convert" a pointer to char to a single char . 你不能(真的)将指向char的指针“转换”为单个char You can however extract one single character from a string. 但是,您可以从字符串中提取一个单个字符。

For example, to get the first character of the first argument to your program, you can do eg 例如,要获取程序的第一个参数的第一个字符,您可以执行以下操作:

char first_char_of_first_arg = 0;
if (argv > 1)
    first_char_of_first_arg = argv[1][0];

the code contains several problems 代码包含几个问题

compiling with all warnings enabled would allow the compiler to display those problems. 在启用所有警告的情况下进行编译将允许编译器显示这些问题。

for gcc, at a minimum, use: '-Wall -Wextra -pedantic' 对于gcc,至少使用:'-Wall -Wextra -pedantic'

Note: warnings need to be fixed, as the compiler knows the C language better than you or I. 注意:警告需要修复,因为编译器比你或我更了解C语言。

#include <stdio.h>

int main(int argc, const char *argv[])
{
    //<< parameter argc not used
    //   and it should be used to assure that argv[1] and argv[2] actually exist

    char argOne = argv[1];
    //<< argv[1] is a pointer to a character string.
    //   argOne is a single char
    //   it should be: 'char argOne = argv[0][0];'

    char argTwo = argv[2];
    //<< argv[2] is a pointer to a character string.
    //   argTwo is a single character
    //   it should be: 'char argTwo = argv[1][0];'
    //<< argTwo is not used

    switch(argOne)
    //<< a character can be treated as an int, but due care needs to be exercised
    {
        case '1234' :
        //<< a case statement can only look at an 'int',
        //   not a pointer to a (in this case unterminated) string
        //   and argOne is a single char, not a pointer to a unterminated string
            printf("1234!\n" );
            break;
        default :
        printf("Invalid\n" );
        //<< for human readability, use consistent indentation
        //   and never use tabs for indenting
        //<< every case should be terminated with 'break;'
        //   unless the case is to fall through to the next case
        //   I.E. just because a syntax 'can' be used does not mean it 'should' be used
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM