www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting pointer to struct in struct declaration from C to D

reply Rick Noether <richard.noether alum.com> writes:
Ok, I'm a bloody neophyte in D

Let's say in a C header, I have 

struct A
{
  unsigned long a;
  unsigned long b;
};

struct B
{
  struct A  x;
  struct C* y;
};


My question is what does the corresponding declaration
look like in D? I'm guessing at


struct A
{
  uint a;
  uint b;
};

struct B
{
  A     x;
  void* y;
};

I'm particularly uncertain about the conversion
of that "struct C* y"?

Thanks in advance,
Rick
Nov 26 2005
parent reply Hasan Aljudy <hasan.aljudy gmail.com> writes:
Rick Noether wrote:
 Ok, I'm a bloody neophyte in D
 
 Let's say in a C header, I have 
 
 struct A
 {
   unsigned long a;
   unsigned long b;
 };
 
 struct B
 {
   struct A  x;
   struct C* y;
 };
 
 
 My question is what does the corresponding declaration
 look like in D? I'm guessing at
 
 
 struct A
 {
   uint a;
   uint b;
 };
 
 struct B
 {
   A     x;
   void* y;
 };
 
 I'm particularly uncertain about the conversion
 of that "struct C* y"?
 
 Thanks in advance,
 Rick
I'm guessing it's C* y;
Nov 26 2005
parent reply Rick Noether <richard.noether alum.com> writes:
On Sat, 26 Nov 2005 17:29:50 -0700 Hasan Aljudy wrote:

 Rick Noether wrote:
 Ok, I'm a bloody neophyte in D
 
 Let's say in a C header, I have 
 
 struct A
 {
   unsigned long a;
   unsigned long b;
 };
 
 struct B
 {
   struct A  x;
   struct C* y;
 };
 
 
 My question is what does the corresponding declaration
 look like in D? I'm guessing at
 
 
 struct A
 {
   uint a;
   uint b;
 };
 
 struct B
 {
   A     x;
   void* y;
 };
 
 I'm particularly uncertain about the conversion
 of that "struct C* y"?
 
 Thanks in advance,
 Rick
I'm guessing it's C* y;
Hi Hasan, thanks for your reply. Sorry if I was unclear. In my example "C" itself is not a declared type, I guess it is simply a name for a pointer to a struct. My C knowledge got very rusty over the years ;-) Rick
Nov 26 2005
parent reply "Kris" <fu bar.com> writes:
"Rick Noether" <richard.noether alum.com> wrote
{snip]
 I'm guessing it's
 C* y;
Hi Hasan, thanks for your reply. Sorry if I was unclear. In my example "C" itself is not a declared type, I guess it is simply a name for a pointer to a struct. My C knowledge got very rusty over the years ;-)
Then, it's not entirely clear what you're asking. In C, something prefixed with "struct" is indeed of struct type. Was the original post of "struct C* y;" a typo?
Nov 26 2005
parent reply Rick Noether <richard.noether alum.com> writes:
On Sat, 26 Nov 2005 16:50:35 -0800 Kris wrote:

 "Rick Noether" <richard.noether alum.com> wrote
 {snip]
 I'm guessing it's
 C* y;
Hi Hasan, thanks for your reply. Sorry if I was unclear. In my example "C" itself is not a declared type, I guess it is simply a name for a pointer to a struct. My C knowledge got very rusty over the years ;-)
Then, it's not entirely clear what you're asking. In C, something prefixed with "struct" is indeed of struct type. Was the original post of "struct C* y;" a typo?
Hi Chris, no, not a typo. And yes, B.y is a pointer to a struct type. But that struct type isn't declared anywhere. When you are going to assign a pointer to some concrete struct Z to B.y you'd have to cast it to C* (or struct C*), of course. When retrieving the value of B.y you'd have to cast it back to Z* (hence you need to know what was put in B.y). It's a horrible design and I don't see any value in it. I even don't know if it's valid C, but it seems to work (at least on VC6). Now I think that my initial conjecture (void*) can't be improved upon. Thanks, Rick
Nov 26 2005
parent reply "Kris" <fu bar.com> writes:
"Rick Noether" <richard.noether alum.com> wrote
 And yes, B.y is a pointer to a struct type. But that
 struct type isn't declared anywhere. When you are going
 to assign a pointer to some concrete struct Z to B.y you'd have
 to cast it to C* (or struct C*), of course. When retrieving
 the value of B.y you'd have to cast it back to Z* (hence you
 need to know what was put in B.y).

 It's a horrible design and I don't see any value in it. I even
 don't know if it's valid C, but it seems to work (at least on VC6).
 Now I think that my initial conjecture (void*) can't be improved
 upon.
Oh, right. Then void* is the right thing, unless you decide to use classes (where the C* would instead be a reference to some base-class).
Nov 26 2005
parent Rick Noether <richard.noether alum.com> writes:
On Sat, 26 Nov 2005 17:54:34 -0800 Kris wrote:

 Oh, right. Then void* is the right thing, unless you decide to use classes 
 (where the C* would instead be a reference to some base-class).
Thanks for your confirmation. I'm interfacing to a legacy C system, so I'll never put D class references in B.y. Instead, I have to figure out the concrete struct type to use depending on the context (method) that gets called (ugly, isn't it?). Rick
Nov 26 2005