R..*_*R.. 288
typedef会是
typedef char type24[3];
Run Code Online (Sandbox Code Playgroud)
但是,这可能是一个非常糟糕的主意,因为结果类型是一种数组类型,但它的用户不会看到它是一个数组类型.如果用作函数参数,它将通过引用传递,而不是通过值传递,并且sizeoffor它将是错误的.
一个更好的解决方案是
typedef struct type24 { char x[3]; } type24;
Run Code Online (Sandbox Code Playgroud)
您可能也希望使用unsigned char而不是char,因为后者具有实现定义的签名.
yst*_*sth 44
你要
typedef char type24[3];
Run Code Online (Sandbox Code Playgroud)
C类型的声明很奇怪.如果声明了该类型的变量,则将类型精确地放在变量名称的位置.
Ger*_*ger 28
来自R ..的回答:
但是,这可能是一个非常糟糕的主意,因为结果类型是一种数组类型,但它的用户不会看到它是一个数组类型.如果用作函数参数,它将通过引用而不是值传递,并且它的sizeof将是错误的.
没有看到它是一个数组的用户很可能会写这样的东西(失败):
#include <stdio.h>
typedef int twoInts[2];
void print(twoInts *twoIntsPtr);
void intermediate (twoInts twoIntsAppearsByValue);
int main () {
twoInts a;
a[0] = 0;
a[1] = 1;
print(&a);
intermediate(a);
return 0;
}
void intermediate(twoInts b) {
print(&b);
}
void print(twoInts *c){
printf("%d\n%d\n", (*c)[0], (*c)[1]);
}
Run Code Online (Sandbox Code Playgroud)
它将使用以下警告进行编译:
In function ‘intermediate’:
warning: passing argument 1 of ‘print’ from incompatible pointer type [enabled by default]
print(&b);
^
note: expected ‘int (*)[2]’ but argument is of type ‘int **’
void print(twoInts *twoIntsPtr);
^
Run Code Online (Sandbox Code Playgroud)
并产生以下输出:
0
1
-453308976
32767
Run Code Online (Sandbox Code Playgroud)
Ste*_*sop 12
数组不能按C中的值作为函数参数传递.
您可以将数组放在结构中:
typedef struct type24 {
char byte[3];
} type24;
Run Code Online (Sandbox Code Playgroud)
然后按值传递,但当然使用它不太方便:x.byte[0]而不是x[0].
您的函数type24_to_int32(char value[3])实际上是通过指针传递,而不是通过值.它完全相同type24_to_int32(char *value),而且3被忽略了.
如果你很高兴通过指针传递,你可以坚持使用数组并执行:
type24_to_int32(const type24 *value);
Run Code Online (Sandbox Code Playgroud)
这将传递指向数组的指针,而不是指向第一个元素的指针,因此您将其用作:
(*value)[0]
Run Code Online (Sandbox Code Playgroud)
我不确定这真的是一种收获,因为如果你不小心写了,value[1]那么就会发生一些愚蠢的事情.
Ger*_*imo 11
要正确使用数组类型作为函数参数或模板参数,请创建一个结构而不是一个typedef,然后添加一个operator[]结构,这样就可以保持数组的功能如下:
typedef struct type24 {
char& operator[](int i) { return byte[i]; }
char byte[3];
} type24;
type24 x;
x[2] = 'r';
char c = x[2];
Run Code Online (Sandbox Code Playgroud)
小智 6
下面是一个简短的示例,说明了为什么 typedef 数组可能会令人困惑地不一致。其他答案提供了解决方法。
#include <stdio.h>
typedef char type24[3];
int func(type24 a) {
type24 b;
printf("sizeof(a) is %zu\n",sizeof(a));
printf("sizeof(b) is %zu\n",sizeof(b));
return 0;
}
int main(void) {
type24 a;
return func(a);
}
Run Code Online (Sandbox Code Playgroud)
这会产生输出
sizeof(a) is 8
sizeof(b) is 3
Run Code Online (Sandbox Code Playgroud)
因为type24作为参数是一个指针。(在 C 中,数组总是作为指针传递。)幸运的是,gcc8 编译器默认会发出警告。