简体   繁体   中英

Access struct inside struct fails

I have a struct A that is defined as follows:

typedef struct A
{
    CvRect B; // rect 
    int C;
    double D;
}
A;

...

In my main program, I grab one item of a sequence of items:

    A *r = (A*) cvGetSeqElem(X, i);

Whenever I try to access rect, I get one of the following errors:

    if (r.rect.width>100 && r.rect.height>100)

error: request for member 'rect' in 'r', which is of non-class type 'A'

or

    if (r->rect->width>100 && r->rect->height>100)

error: base operand of '->' has non-pointer type 'CvRect'

Any idea how to access the struct CvRect->height if it is inside another struct?

Thanks!

r is a pointer to `struct A , which contains a (non-pointer) member B (supposedly rect ?) to a CvRect . So you have to write
r->rect.width

You combine the approaches. r->rect.width , for instance. r is a pointer, thus you should use the -> operator. rect however is not a pointer, so you use . .

In your example, r is a pointer. Therefore, the direct members of r must be accessed via "->", as r->rect .

r->rect (this should be r->B , since that's what you call it in your definition of A) is an object , not a pointer. Therefore, you would access its members with . So r->rect.width .

The code you've posted isn't consistently named, but I think you want something like

if (r->rect.width>100 && r->rect.height>100)

If r is a pointer you need to dereference it to get the rect member, but that member itself is not a pointer.

Any idea how to access the struct CvRect->height if it is inside another struct?

r->B.width

if r is a pointer or

r.B.width

if r is a instance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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