简体   繁体   English

在 C 中转换结构指针

[英]Casting struct pointers in C

when I compiled the following code:当我编译以下代码时:

AVFrameSideData* avfsd=NULL;
avfsd = av_frame_get_side_data(frame, AV_FRAME_DATA_MOTION_VECTORS);
if(avfsd != NULL) {
    int n_mvs = 0;
    if(avfsd->size > 0)
        n_mvs = avfsd->size / sizeof(struct AVMotionVector);
    struct AVMotionVector *mv = (struct AVMotionVector *) (avfsd->data);
    printf("motion vectors of this frame:\n");
    int idx;
    for(idx=0; idx<n_mvs; idx++) {
        printf("mv[%d]:\n", idx);
        if(mv[idx]->source < 0)
            printf("the current macroblock comes from the past\n");
    }
}

my compiler complained:我的编译器抱怨:

video_analysis.c:46:13: error: invalid type argument of ‘->’ (have ‘struct AVMotionVector’)
   if(mv[idx]->source < 0)

Frankly, there's something wrong here:坦率地说,这里有问题:

struct AVMotionVector *mv = (struct AVMotionVector *) (avfsd->data);

mv is a struct pointer, thereof it's not allowed to access through a two dimensional array manner like *mv[]. mv 是一个结构体指针,不允许通过 *mv[] 这样的二维数组方式访问。 However, when I ran it in GDB, I was able to print out things like the following:但是,当我在 GDB 中运行它时,我能够打印出如下内容:

(gdb) print mv[0]
$2 = {source = -1, w = 16 '\020', h = 16 '\020', src_x = 6, src_y = 10, dst_x = 8, dst_y = 8, flags = 0, motion_x = -5, motion_y = 4, 
  motion_scale = 2}

which is exactly what I want to print out...As you can see, mv[0], which can't be accessed from my original code spinet above can now be accessed in GDB mode.这正是我想要打印出来的......正如你所看到的,mv[0],无法从我上面的原始代码spinet访问,现在可以在GDB模式下访问。 How does GDB make this magic happen? GDB 是如何实现这个奇迹的? If I want to access mv[0] in my program just like in GDB, how should I cast those struct pointers?如果我想在我的程序中访问 mv[0] 就像在 GDB 中一样,我应该如何转换这些结构指针? Thanks in advance :-)提前致谢 :-)

Change改变

if(mv[idx]->source < 0)

to

if(mv[idx].source < 0)

Explanation解释

  • mv is a pointer to struct AVMotionVector mv指向struct AVMotionVector指针

  • mv[idx] is a struct AVMotionVector and therefore you need . mv[idx]是一个struct AVMotionVector ,因此您需要. instead of -> .而不是->

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

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