我有一个C
代码在Solaris
机器上工作正常,但是相同的代码有时会产生分段错误,有时会产生不同的输出。
以下是我在两台机器上有所不同的部分:
FILE *inf;
unsigned char *ptr;
unsigned short *ds;
int n, s;
char work[100];
inf = (FILE *) fopen("Filename", "r");
s = fseek(inf, 0, SEEK_SET);
n = fread(work, 1, sizeof(work), inf);
ptr = (unsigned char *)work;
ptr += 8;
count = 0;
ds = (unsigned short *) ptr;
count = *ds;
当我打印计数值时,15
计算机中的Solaris
和768
计算机中的Linux
。
请建议我在Linux机器上的代码中进行更改。
答案 0 :(得分:2)
我不认为(只是)字节序。 15 = 0x000F。 768 = 0x0300。它可能与char的大小有关。尝试在两台机器上打印CHAR_BIT,看看你得到了什么。