a constant pointer to constant data and ...


G

G G

trying to learn to speak the lang. consistently. :)

so the value of this least access privilege, as term by the author, is in using an
array being passed function?

here then a pointer points to a location in memory from which it cannot be changed
and the data at location is not modifiable.

const int *const ptr -- reads ptr is a const pointer to an int const,

this example is taken from "C How to program " Fig7.14

/* Fig. 7.14: fig07_14.c */
/* Attempting to modify a constant pointer to a constant data*/

#include <stdio.h>

int main( void )
{
int x = 5; /* initialize x */
int y; /* define y */

/*
* ptr is a constant pointer to a constant integer. ptr always
* points to the same location; the integer at that location
* cannot be modified
*/

const int *const ptr = &x;

printf( "%d\n", *ptr );

*ptr = 7; /* error *ptr is const; cannot assign new value */
ptr = &y; /* error ptr is const; cannot assign new address */

printf( "%d\n", *ptr );

return 0; /* indicates successful termination */

}

in the program above the value of x cannot be changed using the pointer, but
the value of x can be changed.

in order to have the constant data shouldn't x be declared a constant int.

const int x = 5;

so,

/*
* modified example, original found in "C How to program" , Fig7.14
* int x = 5; is change to const int x 5
* x cannot be modified by pointer or assignment
*/

#include <stdio.h>

int main( void )
{
const int x = 5; /* initialize x */
int y; /* define y */

/*
* ptr is a constant pointer to a constant integer. ptr always
* points to the same location; the integer at that location
* cannot be modified
*/

const int *const ptr = &x;

printf( "%d\n", *ptr );

x = 8; /* error x cannot be modified */

printf( "%d\n", *ptr );

return 0; /* indicates successful termination */

}

....
const char *string = "hello there"; /* hum? string is a pointer to a character const, right? */

i think i'm confusing myself here. this means that the data in not changeable, not that the pointer to the data can't be change to point to another address?
---------
....
(void) someFunction ( const char *ptr );
....
main()
{
const char *string = "hello there";
...
someFunction( string );
...
}

(void) someFunction ( const char *ptr ) //cannot change the array string
{
}
 
Ad

Advertisements

S

Stefan Ram

G G said:
const int *const ptr -- reads ptr is a const pointer to an int const,

»const ptr« above means that the object »ptr« cannot be written to.
»const int« above means that the object »*ptr« cannot be written to.
* ptr is a constant pointer to a constant integer.

No, the int object is not necessarily constant. It also is
not an »integer« (An integer is a mathematical concept
different from a C int object.) That book is mediocre.

#include <stdio.h> /* printf */

int main( void )
{ int object = 3;
int const * const p = &object;
printf( "%d\n", *p );
object = 4; /* changing an object that is constant according to that book */
printf( "%d\n", *p ); }
 
K

Keith Thompson

»const ptr« above means that the object »ptr« cannot be written to.
»const int« above means that the object »*ptr« cannot be written to.


No, the int object is not necessarily constant. It also is
not an »integer« (An integer is a mathematical concept
different from a C int object.) That book is mediocre.

Conflating "const" and "constant" can lead to considerable confusion.
The "const" keyword (as we've discussed at some length here recently)
really means "read-only". The word "constant" usually refers to an
expression that must be evaluated at compile time, such as 42. Although
the "const" keyword is obviously derived from the word "constant",
they're two very different things.

Conflating "int" and "integer" is also dangerous. "int" is just one of
several integer types. On the other hand, since the C standard does use
the word "integer type" to refer to "int", "unsigned int", "long long",
et al, so it's not unreasonable to refer to an object or value of type
int as an "integer".

Still, the statement:

ptr is a constant pointer to a constant integer

would be better written as:

ptr is a read-only pointer to a read-only object of type int
#include <stdio.h> /* printf */

int main( void )
{ int object = 3;
int const * const p = &object;
printf( "%d\n", *p );
object = 4; /* changing an object that is constant according to that book */
printf( "%d\n", *p ); }

"object" is neither const nor constant. The "const" on the declaration
of p means that you can't use p to modify the object it points to, but
you can modify it if you refer to it by name.

One way to think of it is that the expression *p provides a read-only
"view" of a writable object.
 
Ad

Advertisements

B

Ben Bacarisse

The context was this code:

| int x = 5; /* initialize x */
| int y; /* define y */
|
| /*
| * ptr is a constant pointer to a constant integer. ptr always
| * points to the same location; the integer at that location
| * cannot be modified
| */
|
| const int *const ptr = &x;

Conflating "const" and "constant" can lead to considerable confusion.
The "const" keyword (as we've discussed at some length here recently)
really means "read-only". The word "constant" usually refers to an
expression that must be evaluated at compile time, such as 42. Although
the "const" keyword is obviously derived from the word "constant",
they're two very different things.
Still, the statement:

ptr is a constant pointer to a constant integer

would be better written as:

ptr is a read-only pointer to a read-only object of type int

As you know, even that does not really do the job because the object to
which ptr points is not read-only. Qualifications on the type "pointed
to" by a pointer, p, do not tell you about the target object -- they
tell you about the expression *p.

The two uses of const are different. The first does indeed (to all
intents and purposes) make ptr constant, whereas the second says nothing
at all about the constant or read-only status of the target.

Maybe: ptr is a constant pointer providing read-only access to an int.

One way to think of it is that the expression *p provides a read-only
"view" of a writable object.

Indeed. If there were a simple way to say this, we'd all be using it
rather than the short-hand that started this discussion. (I fear my
modest suggestion won't catch on).
 

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

Top