const void * in structure

S

Skybuck Flying

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.
 
R

RaZiel

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.

It's easier to read C++ declarations from right to left when pointers
are involved. So the type is "pointer to constant void".

- RaZ
 
H

Heinrich Wolf

Skybuck Flying said:
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.
 
M

Morris Keesan

[most newsgroups trimmed from reply]

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;".
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top