简体   繁体   English

试图连接两个指针字符,但其中一个正在改变

[英]trying to concat two pointer char but one of them is changing

I am using Raspberry Pi pico with C. I am trying to take 2 different decimal value to char then concat them.我正在使用带有 C 的 Raspberry Pi pico。我正在尝试将 2 个不同的十进制值用于 char 然后连接它们。 I am taking those decimal values from rfid.我正在从 rfid 中获取这些十进制值。 The values coming from rfid are not decimal at first, I convert these values to decimal by processing.来自 rfid 的值起初不是十进制的,我通过处理将这些值转换为十进制。

char *a;
char *b;
a = lltoa(decimal_2, 10);
printf(a);
b = lltoa(decimal, 10);
printf(b);
int newSize = strlen(a) + strlen(b) + 1;
char *newBuffer = (char *)malloc(newSize);
printf("\n");
strcpy(newBuffer, a);
strcat(newBuffer, b);
printf(newBuffer);

The output is: output 是:

999210803000150
150210803000150

The 999 which is a's value is changing. a 的值 999 正在变化。 Why char *a is changing?为什么 char *a 正在改变?

Here is my lltoa function (I took from stack overflow):这是我的 lltoa function(我从堆栈溢出中获取):


char *lltoa(long long val, int base)
{

    static char buf[64] = {0};

    int i = 62;
    int sign = (val < 0);
    if (sign)
        val = -val;

    if (val == 0)
        return "0";

    for (; val && i; --i, val /= base)
    {
        buf[i] = "0123456789abcdef"[val % base];
    }

    if (sign)
    {
        buf[i--] = '-';
    }
    return &buf[i + 1];
}

Inside the lltoa function you have:lltoa function 中你有:

static char buf[64] = {0};

When you define a local variable as static it means there's only one single instance of the variable, shared between all calls to the function.当您将局部变量定义为static时,这意味着该变量只有一个实例,在对 function 的所有调用之间共享。

So when you call lltoa(decimal_2, 10) the contents of buf is set up one way.因此,当您调用lltoa(decimal_2, 10)时, buf的内容以一种方式设置。 Then in the second call lltoa(decimal, 10) you overwrite the contents of buf .然后在第二次调用lltoa(decimal, 10)时覆盖buf的内容。

And since you only have a single buf , both the pointers a and b will both point to this one single buf .并且由于您只有一个buf ,因此指针ab都将指向这一个buf

Since you want to be able to handle different bases you can't use the standard snprintf as I would otherwise suggest, so my recommendation is that you pass a pointer to a large enough buffer as an argument:由于您希望能够处理不同的基础,因此您不能像我建议的那样使用标准snprintf ,因此我的建议是将指向足够大的缓冲区的指针作为参数传递:

char *lltoa(long long val, int base, char *buf, size_t buflen);

Problems include:问题包括:

Only one buffer只有一个缓冲区

See @Some programmer dude .请参阅@Some programmer dude

Buffer too small缓冲区太小

When val == LLONG_MIN and base == 2 , expected output needs a buffer of size 66.val == LLONG_MINbase == 2时,预期 output 需要大小为 66 的缓冲区。

Undefined behavior (UB)未定义的行为(UB)

val = -val; is UB val == LLONG_MIN .是 UB val == LLONG_MIN


Rather than OP's Here is my lltoa function (I took from stack overflow) .而不是 OP 的这是我的 lltoa function(我从堆栈溢出中获取)

Single buffer alternative单一缓冲区替代

Passed in buffer alternative .通过缓冲区替代

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

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