【问题标题】:How to convert two characters into a string?如何将两个字符转换为字符串?
【发布时间】:2021-12-25 00:08:05
【问题描述】:

我已将以下字符串保存到名为 buffer 的 char 数组中:

{app_id:9401}

我想获得两个整数:94 和 01。我想将buffer[8]buffer[9] 转换为第一个整数(94)和buffer[10]buffer[11] 转换为第二个整数(1 )。我正在尝试这样做,但我得到了分段错误。为什么?

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

int main( void )
{
    char buffer[] = "{app_id:9401}";

    printf("First number:%c%c. Second Number: %c%c\n", buffer[8], buffer[9], buffer[10], buffer[11]);

    char first_number[10] = "";
    strcat(first_number, buffer[8]);
    strcat(first_number, buffer[9]);

    int x = atoi(first_number);
    printf("%d\n", x);
}

【问题讨论】:

  • C 指定所有数字字符都应连续编码,这意味着您可以从任何数字字符中减去'0' 以获得其等效整数(即'9' - '0' == 9)。那么将所有四个数字放在一起只是一个十进制算术问题。
  • 寻求调试帮助的问题通常应包括minimal reproducible example,其中包括函数main 和所有必要的#include 指令。如果您对输入进行硬编码,例如使用char buffer[] = "{app_id:9401}";,然后将发布的代码应用到函数main,您是否能够重现错误?如果是,请将其发布为minimal reproducible example
  • 由于您没有回复我的minimal reproducible example 请求,我现在已将您发布的代码转换为自己的代码。如果您对我的更改不满意,请随时 edit 提出您的问题。

标签: c string char integer atoi


【解决方案1】:

[...] 但我得到分段错误。为什么?

函数strcat 要求两个函数参数都是指向字符串的指针。但是,您没有提供指针作为函数参数。相反,您传递的是单个字符的值。只要您启用了警告,任何好的编译器都应该警告您。我建议您确保始终启用它们。有关详细信息,请参阅此问题:

Why should I always enable compiler warnings?

这个无效的函数参数可能是你的分段错误的原因。

使用函数strcat 可能不是你想要的,因为你想将单个字符连接到一个字符串,而不是一个字符串到一个字符串。 C 标准库不提供执行此操作的函数。但是,您可以自己轻松实现这样的功能:

void concatentate_string_with_character( char *dest, char c )
{
    //calculate length of destination string
    size_t length = strlen( dest );

    //overwrite terminating null character with new character
    dest[length] = c;

    //write new terminating null character
    dest[length+1] = '\0';
}

如果您在代码中将函数 strcat 替换为 concatentate_string_with_character,那么它将起作用:

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

void concatentate_string_with_character( char *dest, char c )
{
    //calculate length of destination string
    size_t length = strlen( dest );

    //overwrite terminating null character with new character
    dest[length] = c;

    //write new terminating null character
    dest[length+1] = '\0';
}

int main( void )
{
    char buffer[] = "{app_id:9401}";

    char first_number[10] = "";
    concatentate_string_with_character( first_number, buffer[8] );
    concatentate_string_with_character( first_number, buffer[9] );
    int x = atoi( first_number );
    printf( "%d\n", x );
}

这个程序有正确的输出94

但是,与其为此使用单独的函数,不如自己简单地定义first_number 的三个元素可能更容易:

#include <stdio.h>
#include <stdlib.h>

int main( void )
{
    char buffer[] = "{app_id:9401}";

    char first_number[] = { buffer[8], buffer[9], '\0' };

    int x = atoi( first_number );
    printf( "%d\n", x );
}

这个程序也有正确的输出94

【讨论】:

    【解决方案2】:

    这是一种方法,例如。从字符串中扫描两个整数,每个整数的最大长度为 2。

    #include <stdio.h>
    
    int main(void)
    {
        char buffer[] = "{app_id:9401}";
        int x, y;
        if(sscanf(buffer + 8, "%2d%2d", &x, &y) == 2)
            printf ("x=%d y=%d\n", x, y);
        return 0;
    }
    

    输出:

    x=94 y=1
    

    如果不知道数字在字符串中的位置,可以用冒号查找

    char *cptr = strchr(buffer, ':');
    

    【讨论】:

    • 虽然这个答案为 OP 的问题提供了一个很好的解决方案,但它并没有回答 OP 的问题,为什么 OP 的代码会导致分段错误。 (不过,我仍然支持答案。)
    猜你喜欢
    • 2011-12-22
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多