View mode: basic / threaded / horizontal-split · Log in · Help
November 27, 2005
Converting pointer to struct in struct declaration from C to D
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
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
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;
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
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
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
"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?
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
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
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
"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).
November 27, 2005
Re: Converting pointer to struct in struct declaration from C to D
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
Top | Discussion index | About this forum | D home