Lesson 10: Pointers and Addresses

In this lesson, you examine one of the most sophisticated features of the C programming language: pointers. In fact, the power and flexibility that C provides in dealing with pointers serve to set it apart from many other programming languages.

Pointers enable you to effectively represent complex data structures, to change values passed as arguments to functions, to work with the memory that has been allocated "dynamically", and to more concisely and efficiently deal with arrays.

 

 

 

 

 

Pointer and Constant

To use const:

  1. If the word const appears to the left of the asterisk, what is pointed to is constant.
  2. If the word const appears to the right of the asterisk, the pointer itself is constant.
  3. If const appears on both sides, both are constant.

 

 

For example:

const int *p ⇒ *p is read-only. [ p is a pointer to an integer constant ] same as a constant integer.

int const *p ⇒ *p is read-only. [ p is a pointer to a constant integer ]

 

int *p const ⇒ Syntax error

int * const p ⇒ p is read-only. [p is a constant pointer to an integer]. Since pointer p here is read-only, the initial value should be assigned when the p is declared,

 

const int *p const ⇒ Syntax error.

const int const *p ⇒ *p is read-only. [ p is a constant pointer to a constant integer]

const int * const *p ⇒ *p and p are read-only. [p is a constant pointer to a pointer to a constant integer.]

 

int const *p const ⇒ Syntax error.

int const int *p ⇒ Syntax error.

int const const *p ⇒ *p is read-only and equivalent to int const *p

int const * const p ⇒ *p and p are read-only. [ p is a constant pointer to a const integer] Since pointer p here is read-only, the initial value should be assigned when the p is declared,

 

Just for the sake of completeness for C following the others explanations, not sure for C++.

pp - pointer to pointer
p - pointer
data - the thing pointed, in examples x
bold - read-only variable

Pointer
p data - int *p;
p data - int const *p;
p data - int * const p;
p data - int const * const p;

Pointer to pointer
pp p data - int **pp;
pp p data - int ** const pp;
pp p data - int * const *pp;
pp p data - int const **pp;
pp p data - int * const * const pp;
pp p data - int const ** const pp;
pp p data - int const * const *pp;
pp p data - int const * const * const pp;

// Example 1
int x;
x = 10;
int *p = NULL;
p = &x;
int **pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 2
int x;
x = 10;
int *p = NULL;
p = &x;
int ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 3
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 4
int const x = 10; // Definition must happen during declaration
int const * p = NULL;
p = &x;
int const **pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 5
int x;
x = 10;
int * const p = &x; // Definition must happen during declaration
int * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 6
int const x = 10; // Definition must happen during declaration
int const *p = NULL;
p = &x;
int const ** const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);
// Example 7
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const *pp = NULL;
pp = &p;
printf("%d\n", **pp);
// Example 8
int const x = 10; // Definition must happen during declaration
int const * const p = &x; // Definition must happen during declaration
int const * const * const pp = &p; // Definition must happen during declaration
printf("%d\n", **pp);

N-levels of Dereference

Just keep going, but may the humanity excommunicate you.
int x = 10;
int *p = &x;
int **pp = &p;
int ***ppp = &pp;
int ****pppp = &ppp;
printf("%d \n", ****pppp);