unsighed char * 和 char *指针的区别

代码如下

#include <stdio.h>

typedef unsigned char *byte_pointer;

void show_bytes(byte_pointer start,int len)
{
    int i;
    for(i = 0 ; i< len;i++)
    {
        printf("%.2x ",start[i]);
    }
    printf("\n");
}
int main(int argc,char* argv[])
{
    char a = 22;
    short b = 222;
    int c = 2222;
    long d = 22222;
    float e = 22;
    double f = 2222;
    long double g = 22222;
    
    show_bytes((byte_pointer) &a, sizeof(char));
    show_bytes((byte_pointer) &b, sizeof(short));
    show_bytes((byte_pointer) &c, sizeof(int));
    show_bytes((byte_pointer) &d, sizeof(long));
    show_bytes((byte_pointer) &e, sizeof(float));
    show_bytes((byte_pointer) &f, sizeof(double));
    show_bytes((byte_pointer) &g, sizeof(long double));
    
}

 

结果是

16
de 00
ae 08 00 00
ce 56 00 00
00 00 b0 41
00 00 00 00 00 5c a1 40
00 00 00 00 00 00 9c ad 0d 40 00 00

改变一下第三行

typedef char *byte_pointer;

 

结果是

16
ffffffde 00
ffffffae 08 00 00
ffffffce 56 00 00
00 00 ffffffb0 41
00 00 00 00 00 5c ffffffa1 40
00 00 00 00 00 00 ffffff9c ffffffad 0d 40 00 00

 

PS:程序是在MSYS上编译的,gcc编译器,用insight1.5无法调试(显示Undefined Command "typedef")

我在fedora15上用gcc编译过,结果是一样的

 

请不吝赐教!!

 

 

c

1个回答

printf("%.2x ",start[i]);

这句话会把char转化为int,如果是有符号数,会拓展符号位,就打印出ffff之类的

立即提问