What does Const mean in this c structure ? and what is the delphi equivalent
?
I think const struct just means it can't be modified... is that correct ?
Struct {
Type1 Field1;
Const struct Type2 *Field2;
} Structure1;
So my guess would be:
Type
Structure1 = record
Field1 : Type1;
Field2 : ^Type2;
end;
Delphi doesn't have constant fields in structures/records ?
Why would someone make the field const in C, is it really that important ?
I am guessing it's just a safety precaution during coding... ?
Bye,
Skybuck
That is a pointer to a const struct. The value of the pointer field may
change, but that value may not be used to modify the thing being pointed
at. Delphi has no equivalent.
> Why would someone make the field const in C, is it really that important ?
I could try to answer, but since you've cross-posted this to a C
newsgroup, I'll let the experts there explain that. I don't know how
similar C and C++ are in this regard, but you may be interested in the
C++ FAQ category on "const correctness":
http://www.parashift.com/c++-faq-lite/const-correctness.html
--
Rob
> What does Const mean in this c structure ? and what is the delphi equivalent
> ?
>
> Struct {
> Type1 Field1;
> Const struct Type2 *Field2;
> } Structure1;
>
Nothing I'am aware of. 'Const' is not a C-word. If you are talking of
'const', it's a different story.It means that the pointer Field2 is
only allowed to make read acces to the pointee.
A big difference between Pascal and C : C is case sensitive.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
"C is a sharp tool"
Well now I'm confused.
Does it mean
1. The pointer is read only.
2. The structure is read only.
?
:)
The second one.
--
Rob
> Does it mean
>
> 1. The pointer is read only.
> 2. The structure is read only.
It means that the 'struct Type2' pointed by 'Field2' is read only when
you use Field2 to access it.
Lot's of good answers below this, but...
Here is another:
const struct Type2 *Field2; // Field2 is a pointer to a const struct Type2.
struct Type2 * const Field2; // Field2 is a const pointer to a struct Type2.
In the first the struct is read only. In the second the pointer is readonly.
-Rich
P.S. Am I the only one who has found reading declarations from the
inside out helps in C? e.g:
int (*f)[10]; // f is a pointer to an array of 10 int.
You have to understand the rules of "inside out" unfortunately.
--
Richard Pennington
Email: ri...@pennware.com
http://www.pennware.com ftp://ftp.pennware.com
so:
imagine we define somewhere,
struct Type2 {
int foo;
};
int bar;
struct Structure1 s1;
bar = s1.Field2->foo; //Valid because read-only?
s1.Field2->foo = bar; //Non valid because Field2 is read-only through
s1.Field2
struct Type2 *s2_;
s2 = s1.Field2; // Is it valid? There is an implicit const cast
here
s2->foo = bar; // if previous line ok, it is an access to
s1.Field2 but via // a different pointer.
If the example given is valid (the structure that s1.Field2 points at
can be modified provided you do not use the s1.Field2 access), could
the restrict keyword help the programmer in having a truly read-only
part of Structure1?
Thomas