Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

const field in structure

5 views
Skip to first unread message

Skybuck Flying

unread,
Aug 11, 2004, 2:26:25 PM8/11/04
to
Hello,

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


Rob Kennedy

unread,
Aug 11, 2004, 2:46:14 PM8/11/04
to
Skybuck Flying wrote:
> 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;

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

Emmanuel Delahaye

unread,
Aug 11, 2004, 4:03:16 PM8/11/04
to
Skybuck Flying wrote on 11/08/04 :

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

Skybuck Flying

unread,
Aug 11, 2004, 4:51:19 PM8/11/04
to

"Emmanuel Delahaye" <em...@YOURBRAnoos.fr> wrote in message
news:mn.5d2b7d48f...@YOURBRAnoos.fr...

> Skybuck Flying wrote on 11/08/04 :
>
> > 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.

Well now I'm confused.

Does it mean

1. The pointer is read only.
2. The structure is read only.

?

:)


Message has been deleted

Rob Kennedy

unread,
Aug 11, 2004, 5:08:06 PM8/11/04
to
Skybuck Flying wrote:
> Does it mean
>
> 1. The pointer is read only.
> 2. The structure is read only.

The second one.

--
Rob

Emmanuel Delahaye

unread,
Aug 11, 2004, 5:44:02 PM8/11/04
to
Skybuck Flying wrote on 11/08/04 :
>>> Struct {
>>> Type1 Field1;
>>> const struct Type2 *Field2; /* -ed- qualifier fixed */
>>> } Structure1;

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

Richard Pennington

unread,
Aug 11, 2004, 10:36:41 PM8/11/04
to
Skybuck Flying wrote:

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

Thomas L.

unread,
Aug 12, 2004, 3:32:03 AM8/12/04
to
Emmanuel Delahaye <em...@YOURBRAnoos.fr> wrote in message news:<mn.5d907d486...@YOURBRAnoos.fr>...

> Skybuck Flying wrote on 11/08/04 :
> >>> Struct {
> >>> Type1 Field1;
> >>> const struct Type2 *Field2; /* -ed- qualifier fixed */
> >>> } Structure1;
>
> > 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.

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

0 new messages