C语言 typedef 定长数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4523497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:24:14  来源:igfitidea点击:

typedef fixed length array

carraysgcctypedef

提问by 341008

I have to define a 24-bit data type.I am using char[3]to represent the type. Can I typedef char[3]to type24? I tried it in a code sample. I put typedef char[3] type24;in my header file. The compiler did not complain about it. But when I defined a function void foo(type24 val) {}in my C file, it did complain. I would like to be able to define functions like type24_to_int32(type24 val)instead of type24_to_int32(char value[3]).

我必须定义一个 24 位数据char[3]类型。我用来表示类型。我可以的typedefchar[3]type24?我在代码示例中尝试过。我把typedef char[3] type24;我的头文件。编译器没有抱怨它。但是当我void foo(type24 val) {}在我的 C 文件中定义一个函数时,它确实抱怨。我希望能够定义函数,type24_to_int32(type24 val)而不是type24_to_int32(char value[3]).

回答by R.. GitHub STOP HELPING ICE

The typedef would be

typedef 将是

typedef char type24[3];

However, this is probably a very bad idea, because the resulting type is an array type, but users of it won't see that it's an array type. If used as a function argument, it will be passed by reference, not by value, and the sizeoffor it will then be wrong.

然而,这可能是一个非常糟糕的主意,因为结果类型是一个数组类型,但它的用户不会看到它是一个数组类型。如果用作函数参数,它将按引用传递,而不是按值传递,然后sizeoffor 它就会出错。

A better solution would be

更好的解决方案是

typedef struct type24 { char x[3]; } type24;

You probably also want to be using unsigned charinstead of char, since the latter has implementation-defined signedness.

您可能还想使用unsigned char代替char,因为后者具有实现定义的签名。

回答by ysth

You want

你要

typedef char type24[3];

C type declarations are strange that way. You put the type exactly where the variable name would go if you were declaring a variable of that type.

C 类型声明在这种情况下很奇怪。如果要声明该类型的变量,则将类型准确地放在变量名所在的位置。

回答by Gerhard Burger

From R..'s answer:

R.. 的回答

However, this is probably a very bad idea, because the resulting type is an array type, but users of it won't see that it's an array type. If used as a function argument, it will be passed by reference, not by value, and the sizeof for it will then be wrong.

然而,这可能是一个非常糟糕的主意,因为结果类型是一个数组类型,但它的用户不会看到它是一个数组类型。如果用作函数参数,它将按引用传递,而不是按值传递,并且它的 sizeof 将是错误的。

Users who don't see that it's an array will most likely write something like this (which fails):

没有看到它是一个数组的用户很可能会写这样的东西(失败):

#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]);
}

It will compile with the following warnings:

编译时会出现以下警告:

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);
         ^

And produces the following output:

并产生以下输出:

0
1
-453308976
32767

回答by Steve Jessop

Arrays can't be passed as function parameters by value in C.

数组不能作为函数参数在 C 中按值传递。

You can put the array in a struct:

您可以将数组放入结构中:

typedef struct type24 {
    char byte[3];
} type24;

and then pass that by value, but of course then it's less convenient to use: x.byte[0]instead of x[0].

然后按值传递它,但当然使用起来不太方便:x.byte[0]而不是x[0].

Your function type24_to_int32(char value[3])actually passes by pointer, not by value. It's exactly equivalent to type24_to_int32(char *value), and the 3is ignored.

您的函数type24_to_int32(char value[3])实际上是通过指针传递的,而不是通过值传递的。它完全等同于type24_to_int32(char *value),并且3被忽略。

If you're happy passing by pointer, you couldstick with the array and do:

如果您很高兴通过指针,您可以坚持使用数组并执行以下操作:

type24_to_int32(const type24 *value);

This will pass a pointer-to-array, not pointer-to-first-element, so you use it as:

这将传递一个指向数组的指针,而不是指向第一个元素的指针,因此您可以将其用作:

(*value)[0]

I'm not sure that's really a gain, since if you accidentally write value[1]then something stupid happens.

我不确定这真的是一种收获,因为如果你不小心写了value[1]那么愚蠢的事情就会发生。

回答by Geronimo

To use the array type properly as a function argument or template parameter, make a struct instead of a typedef, then add an operator[]to the struct so you can keep the array like functionality like so:

要将数组类型正确用作函数参数或模板参数,请创建一个结构而不是 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];

回答by Daniel

Here's a short example of why typedef array can be confusingly inconsistent. The other answers provide a workaround.

这是一个简短的例子,说明为什么 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);
}

This produces the output

这产生输出

sizeof(a) is 8
sizeof(b) is 3

because type24 as a parameter is a pointer. (In C, arrays are always passed as pointers.) The gcc8 compiler will issue a warning by default, thankfully.

因为 type24 作为参数是一个指针。(在 C 中,数组总是作为指针传递。)幸运的是,gcc8 编译器默认会发出警告。