2. 2
const and Pointer
const int *
– p is a pointer variable that can hold an address of const
integers
– Cannot assign value 9 to the variable pointed by p, because
the variable which p points to is const int
const int a = 7;
const int *p = &a; // possible
a = 8 ; // impossible
*p = 9 ; // impossible
3. 3
const and Pointer
const int *
– p, which is declared to point to const integers, can also hold an
address of integer type
– a can not be modified through by p, because p is declared as
const int *
• Any variable which is accessed through p is considered as const int
int a = 7;
const int *p = &a; // possible
a = 8 ; // possible
*p = 9 ; // impossible
4. 4
const and Pointer
int * and const int
– p, which is declared to point to normal integers, cannot hold
the address of const integers
– if it is possible, we can change the const variable through p
• Eg) *p = 8
const int a = 7;
int *p = &a; // impossible
5. 5
const and Pointer
int * const
– p is a const pointer variable which can point to normal
integers
– You cannot change its value because it is a const variable
– p is a const pointer variable of normal integer, so p cannot
hold the address of const integers
int a = 7, b = 8 ;
int * const p = &a; // possible
p = &b; // impossible
const int a = 7;
int * const p = &a; // impossible
6. 6
const and Pointer
const int * const
– p is a const pointer variable of const integers, so p can be
initialized once but cannot be changed anymore
const int a = 7, b = 8 ;
const int * const p = &a; // possible
p = &b; // impossible
*p = 6; // impossible
int a = 7, b = 8 ;
const int * const p = &a; // possible
p = &a; // impossible
*p = 6; // impossible
7. typedef
Example
– Type name is too long
– Can we make it shorter?
7
unsigned short var1 ;
unsigned short var2 ;
…
ushort var1 ;
ushort var2 ;
…
8. typedef
typedef
– old name can be consist of many words
– But, new name should be consist of one word.
8
typedef old name new name
typedef unsigned short ushort ;
void main()
{
ushort var1 ;
ushort var2 ;
…
}
9. typedef
Example
9
typedef int Int ;
typedef Int INT ;
typedef unsigned long ulong ;
void main()
{
ulong var1 ;
Int k ;
INT m ;
…
}