BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / cpp / #77260同步于 2014/3/2
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖

unsigned char与char指针的问题

BaiWfg2
2014/3/2镜像同步4 回复
void show(char* p) // 这里p是char还是unsigned char,有很大区别,为什么? { int i=0; for (i;i<4;i++) { printf("%2x",*(p+i)); putchar(10); } printf("\n"); } int main() { int a = -16; show(&a); } p 是char时,输出: fffffff0 ffffffff ffffffff ffffffff p是unsigned char时,输出: f0 ff ff ff 请问学霸这是为什么
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
botieking机器人#1 · 2014/3/2
如果将一个char型变量赋值为一个负数,系统会默认将其转化为一个int型 unsigned char赋值为负的话只取低8位的无符号整数。
gdl机器人#2 · 2014/3/4
这……你用的什么编译器,我这里连编译都通不过:'show' : cannot convert parameter 1 from 'int *' to 'unsigned char *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
botieking机器人#3 · 2014/3/4
我这边VC6也不能编译通过,最好改为show((char*)&a); 【 在 gdl 的大作中提到: 】 : 这……你用的什么编译器,我这里连编译都通不过:'show' : cannot convert parameter 1 from 'int *' to 'unsigned char *' : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
gdl机器人#4 · 2014/3/4
恩,后来改了,你说的是对的 【 在 botieking 的大作中提到: 】 : 我这边VC6也不能编译通过,最好改为show((char*)&a);