sizeof typedef指针 [英] sizeof typedef pointer
问题描述
我有一个这样定义的结构:
I have a struct that defined like that:
typedef struct my_struct
{
int numbers[10];
}
*my_struct;
有没有办法找出它的大小?
Is there a way to find out its size?
sizeof(my_struct);// return size of a pointer
推荐答案
结构类型本身使用struct
进行拼写,因此您可以说:
The struct type itself is spelled with struct
, so you can say:
sizeof (struct my_struct)
如果您还没有给结构命名,那将是不可能的:
This would not work if you hadn't also given your struct a name, which would have been possible:
typedef struct { int numbers[10]; } * foo; /* struct type has no name */
foo p = malloc(1000);
p->numbers[3] = 81;
我会说所有这些都是可怜的代码,它们毫无理由地简洁.我只是保持所有名称唯一,并为所有名称命名,而不是为别名指针命名.例如:
I'd say all of this is poor code that is needlessly terse for no reason. I would just keep all the names unique, and name everything, and not alias pointers, for that matter. For example:
typedef struct my_struct_s my_struct;
my_struct * create_my_struct(void);
void destroy_my_struct(my_struct * p);
struct my_struct_s
{
int numbers[10];
};
所有内容都有唯一的名称,typedef与struct定义分开,并且指针是显式的.
Everything has a unique name, the typedef is separate from the struct definition, and pointers are explicit.
这篇关于sizeof typedef指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!