如何知道类型结构属性的偏移值?
How to know the offset value of type structure property?
我想知道在C语言中如何获取结构体的偏移值
我正在将结构转换为 C#。例如。如果 log_level_offset 的偏移值将为 0,则类似的方式是 属性.
的下一个和其余部分的偏移值
我的结构示例:
typedef struct TestStruct
{
const AVClass *av_class;
int log_level_offset;
enum AVMediaType codec_type;
const struct AVCodec *codec;
void *priv_data;
int bit_rate_tolerance;
uint8_t *extradata;
int extradata_size;
float b_quant_factor;
uint16_t *intra_matrix;
uint64_t channel_layout;
} TestStruct;
指针在c#中为4字节,通常定义为IntPtr。整数和浮点数是 4 个字节。 uint8 是 8 个字节。请参阅下面的结构
[StructLayout(LayoutKind.Sequential)]
public struct TestStruct
{
IntPtr av_class;
int log_level_offset;
int codec_type; //may be different depending on size of c language code
IntPtr codec;
IntPtr priv_data;
int bit_rate_tolerance;
IntPtr extradata;
int extradata_size;
float b_quant_factor;
IntPtr intra_matrix;
ulong channel_layout;
}
要知道结构成员在 c 中的字节位置,请使用 stddef.h 中的 offsetof
宏:
#include <stddef.h>
printf("%zu", offsetof(struct TestStruct, log_level_offset));
(%zu
因为宏 returns 是 size_t
类型的整数。)
请注意,这考虑了潜在的填充字节,因此一个系统上的偏移量不一定与另一个系统上的偏移量相同。
我想知道在C语言中如何获取结构体的偏移值 我正在将结构转换为 C#。例如。如果 log_level_offset 的偏移值将为 0,则类似的方式是 属性.
的下一个和其余部分的偏移值我的结构示例:
typedef struct TestStruct
{
const AVClass *av_class;
int log_level_offset;
enum AVMediaType codec_type;
const struct AVCodec *codec;
void *priv_data;
int bit_rate_tolerance;
uint8_t *extradata;
int extradata_size;
float b_quant_factor;
uint16_t *intra_matrix;
uint64_t channel_layout;
} TestStruct;
指针在c#中为4字节,通常定义为IntPtr。整数和浮点数是 4 个字节。 uint8 是 8 个字节。请参阅下面的结构
[StructLayout(LayoutKind.Sequential)]
public struct TestStruct
{
IntPtr av_class;
int log_level_offset;
int codec_type; //may be different depending on size of c language code
IntPtr codec;
IntPtr priv_data;
int bit_rate_tolerance;
IntPtr extradata;
int extradata_size;
float b_quant_factor;
IntPtr intra_matrix;
ulong channel_layout;
}
要知道结构成员在 c 中的字节位置,请使用 stddef.h 中的 offsetof
宏:
#include <stddef.h>
printf("%zu", offsetof(struct TestStruct, log_level_offset));
(%zu
因为宏 returns 是 size_t
类型的整数。)
请注意,这考虑了潜在的填充字节,因此一个系统上的偏移量不一定与另一个系统上的偏移量相同。