我对这段代码有一些奇怪的警告:
typedef double mat4[4][4];
void mprod4(mat4 r, const mat4 a, const mat4 b)
{
/* yes, function is empty */
}
int main()
{
mat4 mr, ma, mb;
mprod4(mr, ma, mb);
}
Run Code Online (Sandbox Code Playgroud)
gcc 输出如下:
$ gcc -o test test.c
test.c: In function 'main':
test.c:13: warning: passing argument 2 of 'mprod4' from incompatible pointer
type
test.c:4: note: expected 'const double (*)[4]' but argument is of type 'double
(*)[4]'
test.c:13: warning: passing argument 3 of 'mprod4' from incompatible pointer
type
test.c:4:
note: …Run Code Online (Sandbox Code Playgroud) 让L和B成为两台机器.大号为了其位从LSB(最低有效位)到MSB(最高有效位),而乙从MSB以LSB.或者,换句话说,L使用Little Endian而 B使用Big Endian 位 - 不要与字节排序混淆.
问题1已解决:
我们正在编写以下希望可移植的代码:
#include <stdio.h>
int main()
{
unsigned char a = 1;
a <<= 1;
printf("a = %d\n", (int) a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在L上,它会打印2,但B上会发生什么?它会将1移出并打印0吗?
解决方案:在6.5.7的C99定义,说它是,至少在无符号整数类型,<<并>>将于2分别乘法和除法.
问题2:
我们正在编写以下希望可移植的代码:
阅读程序:
/* program READ */
#include <stdio.h>
int main()
{
FILE* fp;
unsigned char a;
fp = fopen("data.dat", "rb");
fread(&a, 1, 1, …Run Code Online (Sandbox Code Playgroud) 我有这个结构,我想写一个文件:
typedef struct
{
char* egg;
unsigned long sausage;
long bacon;
double spam;
} order;
Run Code Online (Sandbox Code Playgroud)
此文件必须是二进制文件,并且必须可由任何具有C99编译器的计算机读取.
我查看了这个问题的各种方法,例如ASN.1,XDR,XML,ProtocolBuffers和许多其他方法,但它们都不符合我的要求:
然后我决定制作自己的数据协议.我可以处理以下整数类型的表示:
以有效,简单和干净的方式(令人印象深刻,没有?).然而, 真正的类型现在很痛苦.
我应该如何阅读float和double从字节流?该标准指出,按位运算符(至少&,|,<<和>>)是
整只的类型,这给我留下了没有希望的.我能想到的唯一方法是:
int sign;
int exponent;
unsigned long mantissa;
order my_order;
sign = read_sign();
exponent = read_exponent();
mantissa = read_mantissa();
my_order.spam = sign * mantissa * pow(10, exponent);
Run Code Online (Sandbox Code Playgroud)
但这似乎并不高效.我也找不着的表示的描述double和float …