试图限制两个指针char,但其中之一正在改变

发布于 2025-02-01 22:31:47 字数 895 浏览 4 评论 0原文

我正在使用Raspberry pi pico和C。我正在从RFID中获取那些小数值。来自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);

输出为:

999210803000150
150210803000150

A的值是999。为什么char *a发生变化?

这是我的lltoa函数(我从堆栈溢出中取出):


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];
}

I am using Raspberry Pi pico with C. I am trying to take 2 different decimal value to char then concat them. I am taking those decimal values from rfid. The values ​​coming from rfid are not decimal at first, I convert these values ​​to decimal by processing.

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 :

999210803000150
150210803000150

The 999 which is a's value is changing. Why char *a is changing ?

Here is my lltoa function (I took from stack overflow):


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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别想她 2025-02-08 22:31:47

lltoa函数中,您拥有:

static char buf[64] = {0};

当将local变量定义为static时,这意味着只有一个一个单个实例调用功能。

因此,当您调用lltoa(Decimal_2,10) buf的内容以一种方式设置。然后在第二个呼叫中lltoa(十进制,10)您覆盖buf的内容。

而且,由于您只有一个buf,所以指针ab都将指向这一单个buf

由于您希望能够处理不同的基础,因此您无法按照我的建议使用标准snprintf,因此我的建议是,您将指针传递给足够大的缓冲区作为参数:

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

Inside the lltoa function you have:

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.

So when you call lltoa(decimal_2, 10) the contents of buf is set up one way. Then in the second call lltoa(decimal, 10) you overwrite the contents of buf.

And since you only have a single buf, both the pointers a and b will both point to this one single 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:

char *lltoa(long long val, int base, char *buf, size_t buflen);
青春如此纠结 2025-02-08 22:31:47

问题包括:

只有一个缓冲区

参见 @Some Progincemer dude dude

缓冲区太小

val == llong_minbase == 2时,预期输出需要大小66。

ub)

val = -val;是ub val == llong_min


而不是OP的这是我的lltoa函数(我从堆栈溢出中取出)

单个缓冲区替代

通过buffer 替代

Problems include:

Only one buffer

See @Some programmer dude.

Buffer too small

When val == LLONG_MIN and base == 2, expected output needs a buffer of size 66.

Undefined behavior (UB)

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


Rather than OP's Here is my lltoa function (I took from stack overflow).

Single buffer alternative

Passed in buffer alternative.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文