gpt4 book ai didi

复制 void 指针 C 的值

转载 作者:行者123 更新时间:2023-11-30 19:44:43 29 4
gpt4 key购买 nike

我的 KS0108 液晶屏上应该打印任何类型的整数的函数有问题。这里的问题是:当我想在第一个开关中将 void* numb 值复制到 void* temp 时 - 什么也没有发生。在第二个开关中,当使用 %I 时,得到 0。 (void* numb 的值约为 48,类型为 _uint8_t)代码:

void glcd_WriteInt(void* numb, type type)
{
void* temp = 0;
int8_t buff[10];
int8_t size = 0;
int8_t flag = 1;
int8_t sign = 1;
switch(type)
{
case _int8_t: memcpy(temp, numb, sizeof(int8_t)); break;
case _int16_t: memcpy(temp, numb, sizeof(int16_t)); break;
case _int32_t: memcpy(temp, numb, sizeof(int32_t)); break;
case _int64_t: /*memcpy(temp, numb, sizeof(int64_t)); break;*/return; //if want 64 - uncomment
case _uint8_t: *(uint8_t*) temp = *(uint8_t*) numb/*memcpy(temp, numb, sizeof(uint8_t))*/; break;
case _uint16_t: memcpy(temp, numb, sizeof(uint16_t)); break;
case _uint32_t: memcpy(temp, numb, sizeof(uint32_t)); break;
case _uint64_t: /*memcpy(temp, numb, sizeof(uint64_t)); break;*/return; //if want 64 - uncomment
}
while(flag)
{
switch(type)
{
case _int8_t:
buff[size] = *(int8_t*) temp % 100;
flag = (*(int8_t*) temp /= 100) != 0;
break;
case _int16_t:
buff[size] = *(int16_t*) temp % 100;
flag = (*(int16_t*) temp /= 100) != 0;
break;
case _int32_t:
buff[size] = *(int32_t*) temp % 100;
flag = (*(int32_t*) temp /= 100) != 0;
break;
case _int64_t:
/*buff[size] = *(int64_t*) temp % 100; //if want 64 - uncomment
flag = (*(int64_t*) temp /= 100) != 0;*/
break;
case _uint8_t:
buff[size] = *(uint8_t*) temp % 100;
flag = (*(uint8_t*) temp /= 100) != 0;
break;
case _uint16_t:
buff[size] = *(uint16_t*) temp % 100;
flag = (*(uint16_t*) temp /= 100) != 0;
break;
case _uint32_t:
buff[size] = *(uint32_t*) temp % 100;
flag = (*(uint32_t*) temp /= 100) != 0;
break;
case _uint64_t:
/*buff[size] = *(uint64_t*) temp % 100; //if want 64 - uncomment
flag = (*(uint64_t*) temp /= 100) != 0;*/
break;
}
if(buff[size] < 0){
buff[size] = -buff[size];
sign = -1;
}
size++;
}

if(sign == -1) glcd_Character((char) 45);
glcd_WIntTab(buff, size);
}

我尝试了带有 memcpy 的版本,如下所示:强制转换。令人惊讶的是,这个功能前几天还可以。

最佳答案

你的主要问题是你有

void *temp = 0;

已声明,并且您尝试写入 NULL 指针,因为您使用 void *temp = 0 对其进行了初始化。

即使你不初始化它,它也不会工作,因为它不是一个有效的指针,所以你要么使用malloc分配内存并将其分配给void *temp 指针,或以这种方式使用 union

union Type {
int8_t _int8_t;
int16_t _int16_t;
int32_t _int32_t;
int64_t _int64_t;
uint8_t _uint8_t;
uint16_t _uint16_t;
uint32_t _uint32_t;
uint64_t _uint64_t;
};

void glcd_WriteInt(void* numb, enum type type)
{
union Type temp;

int8_t buff[10];
int8_t size = 0;
int8_t flag = 1;
int8_t sign = 1;

switch (type)
{
case _int8_t: memcpy(&temp, numb, sizeof(int8_t)); break;
case _int16_t: memcpy(&temp, numb, sizeof(int16_t)); break;
case _int32_t: memcpy(&temp, numb, sizeof(int32_t)); break;
case _int64_t: /*memcpy(&temp, numb, sizeof(int64_t)); break;*/return; //if want 64 - uncomment
case _uint8_t: memcpy(&temp, numb, sizeof(uint8_t))/*memcpy(temp, numb, sizeof(uint8_t))*/; break;
case _uint16_t: memcpy(&temp, numb, sizeof(uint16_t)); break;
case _uint32_t: memcpy(&temp, numb, sizeof(uint32_t)); break;
case _uint64_t: /*memcpy(&temp, numb, sizeof(uint64_t)); break;*/return; //if want 64 - uncomment
}

while (flag)
{
switch (type)
{
case _int8_t:
buff[size] = temp._int8_t % 100;
flag = (temp._int8_t /= 100) != 0;
break;
case _int16_t:
buff[size] = temp._int16_t % 100;
flag = (temp._int16_t /= 100) != 0;
break;
case _int32_t:
buff[size] = temp._int32_t % 100;
flag = (temp._int32_t /= 100) != 0;
break;
case _int64_t:
buff[size] = temp._int64_t % 100;
flag = (temp._int64_t /= 100) != 0;
break;
case _uint8_t:
buff[size] = temp._uint8_t % 100;
flag = (temp._uint8_t /= 100) != 0;
break;
case _uint16_t:
buff[size] = temp._uint16_t % 100;
flag = (temp._uint16_t /= 100) != 0;
break;
case _uint32_t:
buff[size] = temp._uint32_t % 100;
flag = (temp._uint32_t /= 100) != 0;
break;
case _uint64_t:
buff[size] = temp._uint64_t % 100;
flag = (temp._uint64_t /= 100) != 0;
break;
}

if (buff[size] < 0)
{
buff[size] = -buff[size];
sign = -1;
}
size++;
}
if (sign == -1)
glcd_Character((char) 45);
glcd_WIntTab(buff, size);
}

我相信 union 是更好的解决方案。

关于复制 void 指针 C 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27536964/

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