[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

const void * in structure

Skybuck Flying

6/12/2011 12:08:00 AM

Hello,

I come across the following C/C++ code which is a bit unfamiliar to me:

const void *SomeField2; // *

typedef struct SomeStructureName1 {
int SomeField1;
const void *SomeField2; // *
int SomeField3;
} SomeStructureName2;

It seems like a "constant void pointer type"

How to translate this structure to Delphi ?

My guess would be:

type
SomeStructureName1 = record
SomeField1 : integer;
SomeField2 : pointer;
SomeField3 : integer;
end;
SomeStructureName2 = SomeStructureName1;

Also is it just a "syntax" difference or is there a binary difference
between

1. void *
vs
2. const void *

?

Bye,
Skybuck.

2 Answers

Dave \Crash\ Dummy

6/12/2011 7:26:00 AM

0


"Skybuck Flying" <Windows7IsOK@DreamPC2006.com> schrieb im Newsbeitrag
news:3f4f3$4df4035d$5419acc3$13678@cache4.tilbu1.nb.home.nl...
> Hello,
>
> I come across the following C/C++ code which is a bit unfamiliar to me:
>
> const void *SomeField2; // *
>
> typedef struct SomeStructureName1 {
> int SomeField1;
> const void *SomeField2; // *
> int SomeField3;
> } SomeStructureName2;
>
> It seems like a "constant void pointer type"
>
> How to translate this structure to Delphi ?
>
> My guess would be:
>
> type
> SomeStructureName1 = record
> SomeField1 : integer;
> SomeField2 : pointer;

I agree.

> SomeField3 : integer;
> end;
> SomeStructureName2 = SomeStructureName1;
>
> Also is it just a "syntax" difference or is there a binary difference
> between
>
> 1. void *
> vs
> 2. const void *

no binary difference, just read-only.

> ?
>
> Bye,
> Skybuck.

Morris Keesan

6/12/2011 4:42:00 PM

0

[most newsgroups trimmed from reply]

On Sat, 11 Jun 2011 20:07:57 -0400, Skybuck Flying wrote:
> Hello,
>
> I come across the following C/C++ code which is a bit unfamiliar to me:

Which is it? C or C++? They're two different languages.
My answer assumes that it's C code.

>
> const void *SomeField2; // *
>
> typedef struct SomeStructureName1 {
> int SomeField1;
> const void *SomeField2; // *
> int SomeField3;
> } SomeStructureName2;
>
> It seems like a "constant void pointer type"

(void *) means a generic pointer to data, which can be converted to or
from a pointer to any object type.
(const void *) means the same kind of pointer, but it points to
non-modifiable data. So if if SomeField2 actually points to an (int),
then you can write

const int *intp = SomeField2;

giving you a pointer to an int which you can't modify, but you can't
write

int *intp = SomeField2;

because that discards the "const" modifier. When the keyword "const"
appears in the position shown above, it refers to the pointed-to data.
If SomeField were itself a constant (unmodifiable) field, then the
declaration would be written "void * const SomeField2;".
--
Morris Keesan -- mkeesan@post.harvard.edu