gpt4 book ai didi

C : How to send an int type variable to an array of char?

转载 作者:行者123 更新时间:2023-11-30 20:16:54 24 4
gpt4 key购买 nike

我正在尝试在名为buf_wr的字符缓冲区数组中写入一个名为value的整数变量。这是我面临问题的函数:

void receiveData(int n, siginfo_t *info, void *unused) 
{
int nw;
int fd;
char buf_wr[10];
int value;

printf(" \nreceived value %i\n", info->si_int);
value = (info->si_int);
printf("value = %i\n", value);
sprintf(buf_wr, "%i", value);
printf("buf_wr contains : %i\n", buf_wr);
nw = write(fd,buf_wr,strlen(buf_wr));
if (nw < 0)
{
perror("Failed to write the message to the device.");
}
}

当我执行整个代码时,它显示:

received value 260
value = 260
buf_wr contains : 135264

buf_wr 不包含我想要输入的值,即 260

如何解决这个问题?谢谢。

最佳答案

printf("buf_wr contains : %i\n", buf_wr);

您应该使用%s来打印字符串

printf("buf_wr contains : %s\n", buf_wr);

关于C : How to send an int type variable to an array of char?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59390567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com