无法将整数与字符串组合。

huangapple go评论77阅读模式
英文:

Can't combine an integer with a string

问题

我正在尝试将一个整数与一个字符串合并。字符串库中有一个名为strcat()的函数。然而,我不能使用strcat(),因为该函数不接受整数作为参数。

所以我使用了sprintf()函数将整数转换为字符串。在这之后,我使用strcat()函数将两个字符串合并在一起。但我既没有错误消息,也没有输出。

这是我正在处理的代码

#include "stdio.h"
#include "string.h"
#include "stdint.h"

char* header = "ABC";
uint8_t data_length = 10;
char* data_length_str;
uint8_t command = 0xA0;

int main()
{
    sprintf(data_length_str, "%d", data_length);
    strcat(header, data_length_str);
    printf("Header = %s", header);

    return 0;
}

我希望看到一个类似"ABC10"的输出。我还在一个包含微控制器的项目中尝试过这个,但出现了硬错误处理器错误。

这里似乎存在问题是什么?

英文:

I am trying to combine an integer with a string. There is a function called strcat() in string library. However, I can't do it with strcat() because the function does not take an integer as parameter.

So I used sprintf() function to convert an integer to string. After doing it, I combined two strings together using strcat() function. I am getting neither error nor output.

This is the code I am working on:

#include "stdio.h"
#include "string.h"
#include "stdint.h"

char* header = "ABC";
uint8_t data_length = 10;
char* data_length_str;
uint8_t command = 0xA0;

int main()
{
    sprintf(data_length_str,"%d",data_length);
    strcat(header,data_length_str);
    printf("Header = %s",header);

    return 0;
}

I want to see an output like "ABC10".I also tried this in a project that contain microcontroller. I got hard fault handler error.

What seems to be a problem here?

答案1

得分: 4

sprintfstrcat 的目标地址需要标识一个足够大以容纳目标字符串的现有存储区域。像 char data_length_string[20]; 这样的声明将创建一个20字节的存储区域,并表示当 data_length_string 出现在大多数类型的表达式中时,编译器应该替换为一个指针,该指针标识了该存储区域。

char header[40] = "ABC"; 这样的声明会表现得类似,但是40字节的存储区域将用字符 'A', 'B' 和 'C' 进行初始化,然后后面跟着37个零。

请注意,诸如 sprintfstrcat 这样的函数会盲目地假定目标缓冲区足够大以容纳生成的字符串。如果不够大,这些函数很可能会覆盖用于其他用途的信息,例如跟踪函数嵌套情况的信息,后果可能会非常严重。这些函数应仅在可以计算字符串长度的绝对上限,并创建足够大的数组以容纳最坏情况下的情况下使用。

英文:

The destination address passed to sprintf and strcat needs to identify a pre-existing region of storage which is large enough to hold the destination string. A declaration like char data_length_string[20]; will create a 20-byte region of storage, and indicate that when data_length_string appears in most kinds of expression, the compiler should substitute a pointer which identifies that region of storage.

A declaration like char header[40] = "ABC"; will behave similarly, but the 40-byte region of storage will be initialized with the characters 'A', 'B', and 'C', which will then be followed by 37 zeroes.

Note that functions like sprintf and strcat will blindly assume that the destination buffer is large enough to hold the resulting string. If it isn't, the functions will likely overwrite information which is used for other purposes, such as keeping track of how functions are nested, with consequences that are likely to be disastrous. Such functions should only be used in cases where it's practical to compute an absolute an upper bound on the length of strings, and make arrays large enough to accommodate that worst case.

答案2

得分: 0

你可以按照以下方式操作:

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

#define DATA_LENGTH (10U)
#define HEADER_SIZE (3U)
#define DEST_BUFFER_SIZE (HEADER_SIZE + DATA_LENGTH)

char header[HEADER_SIZE] = "ABC";
char data_length_str[DEST_BUFFER_SIZE];

int main()
{
    sprintf(data_length_str, "%d", DATA_LENGTH);
    strcat(header, data_length_str);
    printf("Header = %s", header);

    return 0;
}

你需要为data_length_str分配一些内存,可以动态分配,也可以将其定义为静态数组,如果你知道数据的最大长度。

英文:

You can do something as follows

#include &quot;stdio.h&quot;
#include &quot;string.h&quot;
#include &quot;stdint.h&quot;

#define DATA_LENGTH (10U)
#define HEADER_SIZE (3U)
#define DEST_BUFFER_SIZE (HEADER_SIZE + DATA_LENGTH)

char header[HEADER_SIZE] = &quot;ABC&quot;;
char data_length_str[DEST_BUFFER_SIZE];

int main()
{
    sprintf(data_length_str, &quot;%d&quot;, DATA_LENGTH);
    strcat(header, data_length_str);
    printf(&quot;Header = %s&quot;, header);

    return 0;
}

You will need to assign some memory to data_length_str either dynamically or as a static array. You can also define it as an array if you know the maximum length of your data.

huangapple
  • 本文由 发表于 2023年6月16日 00:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76483839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定