简体   繁体   English

在sizeof()中取消引用指针类型

[英]Dereferencing pointer type when in sizeof()

I have following structure: 我有以下结构:

typedef struct _foo_t {
    int bar;
    float buzz;
    char quux[40];
} *const foo_t;

Is there a way to get the size of structure, like it's done via sizeof(struct _foo_t) , but using only name foo_t ? 有没有办法获得结构的大小,就像通过sizeof(struct _foo_t) ,但只使用名称foo_t I have tried sizeof(*foo_t) , but that does not compile. 我尝试过sizeof(*foo_t) ,但是没有编译。

I don't believe you can do this directly. 我不相信你可以直接这样做。 You'll need to define an intermediate typedef if you want to do this: 如果要执行此操作,则需要定义中间typedef:

typedef struct _foo_t {
    int bar;
    float buzz;
    char quux[40];
} foo_t;

typedef foo_t *const foo_tp;

// sizeof(foo_t) should work

Dereferencing a type doesn't really make sense in C. You can dereference a variable, but not a type. 在C中取消引用类型并不真正有意义。您可以取消引用变量,但不能取消引用类型。 In C++, you can do these kinds of type operations using templates, but that's not really applicable since you indicate the C tag. 在C ++中,您可以使用模板执行这些类型的操作,但由于您指定了C标记,因此不适用。

You could also declare a dummy variable of the appropriate type to invoke sizeof on an expression: 您还可以声明一个适当类型的虚拟变量来调用表达式上的sizeof

foo_tp f;
// sizeof(*f) should work

你可以通过使用这样的复合文字来完成这个:

sizeof(*(foo_t){NULL})

foo_t is a typedef , so it is like a type. foo_t是一个typedef ,所以它就像一个类型。

*foo_t is not a valid expression, because you cannot dereference a type. *foo_t不是有效的表达式,因为您无法取消引用某个类型。

Since it is not a valid expression, you cannot get its size. 由于它不是有效的表达式,因此无法获得其大小。

It is as writing: 这是写作:

typedef int * pointer_to_int_type;

size_t a = sizeof(*pointer_to_int_type);

You cannot use a type name as an operand (not only for sizeof), as an expression requires to have a type (in C, types are no first class citizens like in full OO-languages like Python). 你不能使用类型名作为操作数(不仅仅是sizeof),因为表达式需要有一个类型(在C中,类型不像像Python这样的完整OO语言中的一等公民)。 However, you can use a type-name alone when using the parenthesized version - C11 draft 6.5.3.4#1/2. 但是,在使用带括号的版本时,您可以单独使用类型名称 - C11草案6.5.3.4#1/2。 An understandable restriction. 一个可以理解的限制。

Note that an expression in parenthesis after the sizeof keyword would have different gramatical semantics: the parenthesis are part of the expression, while for a type name, they are part of the sizeof operator (which would work like a function call). 请注意, sizeof关键字后括号中的表达式将具有不同的语法语义:括号是表达式的一部分,而对于类型名称,它们是sizeof运算符的一部分(它可以像函数调用一样工作)。 (I'm really not keen writing a parser for C myself.) (我真的不想为C自己写一个解析器。)

I for myself do normally not typedef pointers. 我自己通常不会输入typedef指针。 That hides the semantics and supports forgetting about their somewhat special (and dangerous) properties. 这隐藏了语义,并支持忘记他们有些特殊(和危险)的属性。 An exception would be an external interface where the pointer is externally used more like a handle than a pointer. 一个例外是外部接口,其中外部使用的指针更像是句柄而不是指针。 But this I really only use at the outer membrane. 但我真的只在外膜使用。

Ok, as I got from the discussion, you are trying to work with opaque pointers. 好吧,正如我从讨论中得到的那样,你正试图使用​​不透明的指针。 But that will not work by defining the struct type and the pointer at once anyway. 但是无论如何都不能通过立即定义结构类型和指针来实现。 You will need to split that: 您将需要拆分:

"hidden_stuff.h":

typedef struct HiddenStruct * const HiddenStructHandle;

"hidden_stuff.c"

#include "hidden_stuff.h"

struct HiddenStruct {
    ...
};

Having the struct in the header will break the encapsulation/hiding. 在头文件中使用结构将破坏封装/隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM