试图限制两个指针char,但其中之一正在改变
我正在使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
lltoa
函数中,您拥有:当将local变量定义为
static
时,这意味着只有一个一个单个实例调用功能。因此,当您调用
lltoa(Decimal_2,10)
buf
的内容以一种方式设置。然后在第二个呼叫中lltoa(十进制,10)
您覆盖buf
的内容。而且,由于您只有一个
buf
,所以指针a
和b
都将指向这一单个buf
。由于您希望能够处理不同的基础,因此您无法按照我的建议使用标准
snprintf
,因此我的建议是,您将指针传递给足够大的缓冲区作为参数:Inside the
lltoa
function you have: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 ofbuf
is set up one way. Then in the second calllltoa(decimal, 10)
you overwrite the contents ofbuf
.And since you only have a single
buf
, both the pointersa
andb
will both point to this one singlebuf
.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:问题包括:
只有一个缓冲区
参见 @Some Progincemer dude dude 。
缓冲区太小
当
val == llong_min
和base == 2
时,预期输出需要大小66。ub)
val = -val;
是ubval == llong_min
。而不是OP的这是我的lltoa函数(我从堆栈溢出中取出)。
单个缓冲区替代
通过buffer 替代。
Problems include:
Only one buffer
See @Some programmer dude.
Buffer too small
When
val == LLONG_MIN
andbase == 2
, expected output needs a buffer of size 66.Undefined behavior (UB)
val = -val;
is UBval == LLONG_MIN
.Rather than OP's Here is my lltoa function (I took from stack overflow).
Single buffer alternative
Passed in buffer alternative.