473,568 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Casting a pointer to an enum

HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c (this is getting really confusing, I know!).I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferenc ing pointer to incomplete type.
"Example://fileA.c<#includ e> "fileB.h"st ruct a *a_struct; if
(a->b->typedefed_en um == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_en um == enum_VALUE
)but to no avail.Thanks,Ro b
Nov 13 '05 #1
3 7008
"Rob Jackson" <ma********@lin eone.net> wrote in message
news:95******** *************** ***@posting.goo gle.com...
HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c
(this is getting really confusing, I know!).
Yes, which is why it's always better to post example code
to illustrate your question. However, I'll take a guess.
See below.
I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferenc ing pointer to incomplete type.
"Example://fileA.c<#includ e> "fileB.h"st ruct a *a_struct; if
(a->b->typedefed_en um == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_en um == enum_VALUE
)but to no avail.

As a result of whatever organizational machinations you're
using with your source files, it seems that you're trying
to tell the compiler to dereference a pointer to a struct
whose complete definition it has not yet parsed. This is
what it means by *incomplete type*. The declaration of
a pointer to an incomplete type is allowed, but the dereference
of such a pointer requires the full type be known, otherwise
not enough information is availabe to identify the actual
type of object resulting from the dereference. Was *that*
confusing? :-)

int main()
{
struct X *p; /* OK, don't need to see 'inside' struct X */

p->a; /* Not OK, what is 'a'? */

return 0;
}
/* in a header or somewhere else not yet parsed */
struct X
{
int a;
};
You need to ensure that the full definition of the struct
is visible at the point you refer to its members.

Well, that's my best guess as to your problem. If you
post a short example demonstrating it, perhaps we can
give a more definitive analysis.

-Mike
Nov 13 '05 #2
Rob Jackson wrote:

HiI've got a struct, known by file A.c, which contains a pointer to
struct B. Struct B is unknown by file A.c (it is declared in C.h), and
contains a typedef enum, which is declared in a file B.h, which is
included in file A.c (this is getting really confusing, I know!).I
want to access the enum in file A.c, but don't want to include file
C.h, for various reasons.I've tried casting the enum, but still get
the error "Dereferenc ing pointer to incomplete type.
"Example://fileA.c<#includ e> "fileB.h"st ruct a *a_struct; if
(a->b->typedefed_en um == enum_VALUE)////fileB.htypedef enum
{enum_VALUE, enum_OTHER } enum_type; //If this actually makes sense
(I've reread it, and can just about get my head around it!), has
anyone got any ideas how to cast it? (or do anything else to access
it).I've tried if ((enum_type) a->b->typedefed_en um == enum_VALUE
)but to no avail.Thanks,Ro b


(What a mess of gibberish! Time to review the formatting
options on your news client, perhaps?)

If I've understood correctly, the essential matter is this:
You've got a pointer to a struct of unknown ("incomplete ") type,
and you're trying to access an element of that struct. You
happen to know the type of the desired element, but that doesn't
help you: without knowledge of how the struct elements are
arranged, you can't[*] find that "known" element amid whatever
else the struct might contain.
[*] Special case: If you don't know the complete layout of
the target struct but you do happen to know that the desired
element is the very first thing in the struct, you can get to
it: Convert the struct pointer to a pointer to the type of
the desired element, and have at it. The very first element
of a struct is guaranteed to start at the same address as the
struct itself.[**]

[**] Special special case: Unless that element is a bit-field,
in which case you're stuck. Bit-fields aren't addressable, and
can't be pointed to.

--
Er*********@sun .com
Nov 13 '05 #3
Eric Sosman <Er*********@su n.com> wrote in message news:<3F******* ********@sun.co m>
What a mess of gibberish!
Oops. Sorry about that. I used google groups, so I don't really know
what went wrong there!
You've got a pointer to a struct of unknown ("incomplete ") type,
and you're trying to access an element of that struct. You
happen to know the type of the desired element, but that doesn't
help you: without knowledge of how the struct elements are
arranged, you can't[*] find that "known" element amid whatever
else the struct might contain.


Yes, I see. That makes sense now that I am fully awake! I assumed that
it did not know what type the enum was (which it didn't), but I didn't
think about the fact that it didn't know what offset the variable was
at.

Thanks for clearing that up,

Rob
Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3449
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A good way to do this is by example. So I will give an example and please tell me what you think: I have a base class A with a virtual destructor,...
3
2994
by: Matt | last post by:
Hi, Recently we had some code like this cause a failure: MyEnum myEnum = (MyEnum) (int) dt; i.e. reading an int out of the database and casting it into a type-safe enum. The thought behind this construct was to enforce type safety and
2
5382
by: babylon | last post by:
I have an enum public enum MyEnum : int { X, Y } I have to do int a = (int) MyEnum.X; can i overload the operator or other means to do something like
1
281
by: Remco | last post by:
Hi, Let me try to simply explain my questions. I've created a portal site with different types of users, e.g. Portal Administrators and Normal Users. One base class SessionUser (has a enum field UserType) and for each type of user a inherited class like SessionMasterUser and SessionNormalUser.
0
1711
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. I'm able to use this DLL without any problems within my test windows app, but not within my windows service (that's when I receive the casting...
8
8603
by: kc | last post by:
I'm trying to pull data from a database that I have no control over the structure of. There's a members table with a column for the member's sex. It just stores the sex as M or F. I'd like to create an enum to store the sex. Is there a way that I can create a method in the Enum to convert M to Male and F to Female, or does this method have...
3
3637
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take...
2
2475
by: NullQwerty | last post by:
Hey folks, So, I've got three enum types: enum enum1 enum enum2 enum enum3 And then I've got a function overloaded three times to accept each enum type: private void func(enum1 myenum){}
4
1976
by: mitdej | last post by:
Hi there, I have an several enum types that starts from a nunmber other than 0. For example: public enum InternalStatus { Pending = 1, Ported = 2, Suspended = 3 } I put this values in a int column of a MSSQL table.
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6277
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5501
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.