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
{
}
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
{
}