structure without any data

T

Tagore

I am trying to understand a source code for a project. It uses a
structure without any body
( variables) defined in it i.e in following form
struct ABC;

then it casts this structure to some pointer variable....
Can some body help me in understanding how this type of structure can
be used for any purpose..

If more infomation needed, I would post part of code where it is
used...
 
T

Tagore

I have searched using grep utility I found no definition of ABC(say)..
It is like this...
// in first file
struct ABC;
struct DEF // in same file
{
.... //some data defined
struct ABC * p;
}E;

//defined in some other file
struct c
{
definition
}D;

//In another file

D * confused= (D*)(DEF1->p); // take DEF here as a object of DEF
defined in first file.

--------------------------------
 
J

James Kuyper

Tagore said:
I have searched using grep utility I found no definition of ABC(say)..
It is like this...
// in first file
struct ABC;
struct DEF // in same file
{
.... //some data defined
struct ABC * p;
}E;

//defined in some other file
struct c
{
definition
}D;

//In another file

D * confused= (D*)(DEF1->p); // take DEF here as a object of DEF
defined in first file.

It's entirely possible that the only place where struct ABC is defined
is inside code that you have no access to. That's pretty much the main
purpose of having an opaque type. Typically, you never see an object of
type 'struct ABC'. All you ever see is pointers to such objects. The
actual objects that those pointers point are created and used by
functions that do know the definition, even though your own code does not.
 
B

Barry Schwarz

I have searched using grep utility I found no definition of ABC(say)..
It is like this...
// in first file
struct ABC;
struct DEF // in same file
{
.... //some data defined
struct ABC * p;
}E;

//defined in some other file
struct c
{
definition
}D;

//In another file

D * confused= (D*)(DEF1->p); // take DEF here as a object of DEF
defined in first file.

I assume you meant DEF1 is a pointer to struct DEF. But D is not a
type; it is an object of type struct c. You really ought to show us
examples of the real code because right now you are just digging the
hole deeper.

Does the project come with a library. It is entirely possible for the
implementer to have decided he never wants you to see the internal
details of struct ABC.

Does it help you understand the how (not the what) to remember that
all pointers to struct must have the same representation and
alignment. Therefore, any pointer to struct can be cast to pointer to
any other struct. (Using it to access a member of the struct is a
different issue. That can, for example, run afoul of alignment.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top