473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

const field in structure

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
Nov 14 '05 #1
9 5351
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-lit...rrectness.html

--
Rob
Nov 14 '05 #2
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"

Nov 14 '05 #3

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@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.

?

:)
Nov 14 '05 #4

On Wed, 11 Aug 2004, Skybuck Flying wrote:

"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote...
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;
[...] Well now I'm confused.

Does it mean
1. The pointer is read only.
2. The structure is read only.
?


It means the structure is read-only. The structure pointed to by
'Field2', that is, of course; not the whole 'Structure1' structure!
For the experts: It surprised me just now to learn that 'gcc -ansi'
actually permits

struct foo {
const int bar;
};

Does this mean what I think it means --- 'bar' can only be given a
value during initialization --- or is this just a quirk of GCC or
of the Standard?

-Arthur
Nov 14 '05 #5
Skybuck Flying wrote:
Does it mean

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


The second one.

--
Rob
Nov 14 '05 #6
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.

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html

"C is a sharp tool"

Nov 14 '05 #7
In article <Pi**********************************@unix48.andre w.cmu.edu>
Arthur J. O'Dwyer <aj*@nospam.andrew.cmu.edu> writes:
For the experts: It surprised me just now to learn that 'gcc -ansi'
actually permits

struct foo {
const int bar;
};
This fragment is strictly conforming. The "bar" member of a "struct
foo" is const-qualified and thus read-only. (Note that this is
different from the original example, which -- after spelling
corrections -- declared one member as "pointer to const struct
....", i.e., a read/write pointer to a read-only object.)
Does this mean what I think it means --- 'bar' can only be given a
value during initialization --- or is this just a quirk of GCC or
of the Standard?


The member named "bar" can indeed only be given a value via
initialization (in strictly conforming code anyway -- in practice,
"going behind the compiler's back" to write on the member tends
to "work", for some definition of "work" anyway).

Consider a larger structure:

struct S {
int a;
const int b;
int c;
};

An object of type "struct S" has three members named a, b, and c,
with a and c being read/write and b being read-only -- but by the
other rules about structures, the address of b has to be "between"
that of a and c. On conventional machines with page-granular memory
protection (e.g., 4096 or more bytes at a time are either read/write
or read-only), a "struct S" object will have to occupy wholly-read/write
memory. Thus, "sneaky" code such as:

struct S s_obj = { 1, 2, 3 };
...
int *p = &s_obj.a;
p[1] = 99; /* UNDEFINED, but tends to compile and run anyway */

will tend to overwrite s_obj.b (note that I have assumed there
is no padding here!). The compiler is of course allowed to *assume*
that s_obj.b has not changed (in this case) so whether you can
*see* the change tends to be optimization-level-dependent, for
instance. (In other words, "don't do this". :-) )

On another note, many embedded-systems C programmers like to
use volatile-qualified types for hardware register layout
data structures:

struct fooreg {
volatile int csr;
volatile int dar;
/* etc */
};

This is also valid, Standard C (although the precise meaning of
"volatile" is up to the compiler anyway). I do not share their
enthusaism: I prefer to make the structure contain ordinary,
unqualified types, and then have the pointer that points to that
structure carry the qualifier:

struct fooreg {
int csr;
int dar;
/* etc */
};
...
volatile struct fooreg *reg = (volatile struct fooreg *)addr;

I have two reasons for this, one of which I think is easier to
argue for: the unqualified "struct" version allows you to copy the
hardware registers to software data structures for debugging, and
yet get the software-structure access optimized without having the
optimization interfere with real hardware access. (As it turns
out, to make device drivers portable across widely different
architectures, it is often a good idea not to use such structures
in the first place -- at least not directly, anyway.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #8
Skybuck Flying wrote:
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@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.

?

:)


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

Nov 14 '05 #9
Emmanuel Delahaye <em***@YOURBRAnoos.fr> wrote in message news:<mn***********************@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
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
1
by: Mas | last post by:
I have 3 different structures with each structure having the same first member (int type). I would like every structure of type 1 to have a 1 in the type field, every structure of type 2 to have 2...
5
by: Bill Pursell | last post by:
Suppose I have a structure with many members, and I pass a pointer to that structure to a function. I'd like the prototype of the function to specify that it will only be changing certain members...
15
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
39
by: Leonardo Korndorfer | last post by:
Hi, I'm litle confused by the const modifier, particularly when use const char* or char*. Some dude over here said it should be const char when you dont modify it content inside the function, I...
9
by: raylopez99 | last post by:
I'm posting this fragment from another thread to frame the issue clearer. How to pass an object to a function/method call in C# that will guarantee not to change the object?* In C++, as seen...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.