Notes : C++ – Part 3

< Previous (C++ – Part 2) | (C++ – Part 4) Next >

NOTIFICATION: These notes are published for educational purposes. Using these notes is under your own responsibility and risk. These notes are given ‘as is’. I do not take responsibilities for how you use them.

PDF Content:

  • Const correctness (continued)
  • Const member function
  • Pointers
  • Pointers and Arrays
  • Array of pointers
  • Pointers vs. Arrays
  • Allocating a memory
  • Data type class pointer
  • Passing to function by reference using pointers
  • Memory leaks
  • Pointers with increase and decrease operators
  • Pointers to pointers
  • Void pointers
  • Null pointer
  • Pointers to functions
  • Pointers to members
  • Pointer to member operators .* and ->*
  • Casting pointers to members
  • Difference between .* and ->* operator
  • Convert a pointer-to-member-function to void
  • Maps

Cplusplus_3

 

< Previous (C++ – Part 2) | (C++ – Part 4) Next >

 

 

Share

Pointers with Arrays in C/C++

In the previous post, “Pointers in C/C++”, we talked about pointers in C/C++. We recommend to read this posting before continuing.

Lets say we declare an array of 5 elements:

int i_array[5];

Also, we could declare and instantiate each elements in the array at the same time:

int i_array[5] = {10, 20, 30, 40, 50};

Now, lets assume we created the array and we want to change the values of each element, one way could be:

i_array[0] = 15;
i_array[1] = 25;
i_array[2] = 35;
i_array[3] = 45;
i_array[4] = 55;

However, there is another way to change the values of elements in an array, and that way is by using pointers.

To begin with, first we need to understand how are the array build, each element in the array have an address in memory, the name that we give to the array is connected to the address of the first element. Each consecutive elements will have an address that is the previous address plus the size of the address. For example, if you have an array of characters, each character have a size of 8 bits (1 byte), this means that each element’s address will be different by 1 byte.

For example, this  code would print the address of each element in an array:

int main(int argc, char* argv[]){
  int c_array[5] = {'a', 'b', 'c', 'd', 'e'};
  int index;
  for (index = 0; index < 5; ++index){
    printf('c_array[%d] with value %d has address 0x%X \n',
     	     index, c_array[index], (unsigned int) &c_array[index]);
  }
  return 0;
}

This will show us:

c_array[0] with value a has address 0xBF95EAB7
c_array[1] with value b has address 0xBF95EAB8
c_array[2] with value c has address 0xBF95EAB9
c_array[3] with value d has address 0xBF95EABA
c_array[4] with value e has address 0xBF95EABB

As you can see they are separated by one byte:

0xBF95EAB7 - 0xBF95EAB8 = 1

The graphic representation of this array is:

When working with pointers, we can access to the information indirectly by just using the address of each element in the array. The following code will make a pointer to point at the first element of the array:

char* p_c_array = c_array; /* Not need &. c_array return an address. c_array[0] return a value */

The reason we are not using & when assigning the address is that arrays are accessed by reference, which means that the compiler will return the address of the first element if we write c_array while if we write c_array[0] it will return the value in that position.

If we want to print the first element:

printf('First Element: %c', *p_c_array);

If we want to print the second element, we need to increase the pointer so it will point to the next element.
By increasing it means that we must increase a total amount of one byte (because we are talking about char variables) to access to the next element:

0xBF95EAB7 + 1 byte =  0xBF95EAB8 

There are two ways to increase value in the pointer by one byte:
One ways is:

p_c_array = p_c_array + sizeof(char); /* Size of char return 1 byte */

Or by letting the compiler to increase the value:

p_c_array++;

Just take in consideration that you don’t wish to loose the original address to which you are pointing to the first element of the array; therefore it common practice to create a second pointer that will have the same address and increase that pointer, for example:

char* p_c_array = c_array;  /* Point to first element of array */
char* p_c_array_2;
p_c_array2 = p_c_array;      /* Copy address stored in p_c_array */
p_c_array2++;                /* Increase address stored by one byte to point to next element */
printf('First element: %c. Second Element %c \n',
       *p_c_array,
       *p_c_array2);

The follow example would print all the elements of the array:

int main(int argc, char* argv[]){
  char c_array[5] = {'a', 'b', 'c', 'd', 'e'};
  char* p_c_array = c_array;
  char* p_c_array_2;
  for (p_c_array_2 = p_c_array;
       *p_c_array_2 != '\0';
       ++p_c_array_2){
    printf('p_c_array_2 points to variable with value %c \n',
           *p_c_array_2);
  }
  return 0;
}

Notice that we are using in this case ‘\0’ to indicate the end of the array. This do not apply if we would be using an array of integers for example.

This code will print to screen:

p_c_array_2 points to variable with value a
p_c_array_2 points to variable with value b
p_c_array_2 points to variable with value c
p_c_array_2 points to variable with value d
p_c_array_2 points to variable with value e

We this we had cover the basic about pointers and arrays.

Next post, we are going to talk about pointer functions

Share

Pointers in C/C++

When programming in C and/or C++, you will encounter the use of pointers.
We are going to begin first doing a review of regular pointers and then we will see how to create and use function pointers.

In C/C++, when we create a static or dynamic variable, this variable will have an address in memory. For example:

If we write the following code:

int integer_variable;

An space in memory of the integer size (16-bits, 32-bits, or 64-bits depending the system) will be reserved.

When we assign a value to this variable, this value is store in the memory as a binary number that is represented base on the variable’s type.

int integer_variable = 4;
integer_variable = 5; /* Change value */

If we wish to know in which address is this value store we use ‘&’ in front of the variable.

int main(int argc, char* argv[]){
  int i_variable = 22;
  printf('i_variable has value %d at address 0x%X\n', i_variable, (unsigned int) &i_variable);
  return 0;
}

First note that we are using a downcasting “(unsigned int)” since there are no negative values in memory address else you will receive a warning from the gcc compiler. Second, in this example we are using %X to indicate printf to represent the address as an hexadecimal value.

This will give us:

i_variable has value 22 at address 0xBF9EFC28

If this would be a house for example, i_variable would be the name of the resident while the memory address would be the address of the house.

When creating a pointer in C/C++, we are declaring a variable (which have its own address) that will hold an address.

int* p_i_variable;

And we instantiate this pointer variable with the address of another variable, in this example would be i_variable.

int* p_i_variable = &i_variable;

or you could instantiate in this way:

int* p_i_variable;
p_i_variable = &i_variable;

So, we could say that the pointer p_i_variable is like a P. O. Box which many home-owners use when having companies. The P. O. Box have an address but the content of it is pointed to the real address of the owner.

If we want to ask where the p_i_variable (P.O. Box) is “pointing at” we can do:

int main(int argc, char* argv[]){
  int i_variable = 22;
  int* p_i_variable = &i_variable;
  printf('i_variable with value %d has address 0x%X \n', i_variable, (unsigned int) &i_variable);
  printf('p_i_variable (P.O Box with address 0x%X) is pointing to address 0x%X\n',
         (unsigned int) &p_i_variable,
         (unsigned int) p_i_variable);
}

You may notice that the first parameter is (unsigned int) &p_i_variable, this is to display the address of the pointer variable, while (unsigned int) p_i_variable is to display the value store in that pointer variable which would be the address of i_variable. Here is an example of the output:

i_variable with value 22 has address 0xBF877448
p_i_variable (P.O Box with address 0xBF877444) is pointing to address 0xBF877448

Lets say that want to change the value stored in i_variable. You could changed the value directly by using the variable name:

i_variable = 50;

Or you could do it indirectly by using the pointer:

*p_i_variable = 50;

First notice that we are using the asterisk ‘*’ before the name of the pointer. This tells the compiler the following: To the variable that have the address stored in p_i_variable, we wish to change the value to 50;

For example:

int main(int argc, char* argv[]){
  int i_variable = 22;
  int* p_i_variable = &i_variable;
  printf('i_variable with value %d has address 0x%X \n', i_variable, (unsigned int) &i_variable);
  printf('p_i_variable (P.O Box with address 0x%X) is pointing to address 0x%X\n',
         (unsigned int) &p_i_variable,
         (unsigned int) p_i_variable);

  /* Change value of i_variable directly */
  i_variable = 50;
  printf('i_variable with value %d has address 0x%X \n', i_variable, (unsigned int) &i_variable);

  /* Change value of i_variable indirectly using the pointer p_i_variable */
  *p_i_variable = 100;
  printf('p_i_variable (P.O Box with address 0x%X) is pointing to address 0x%X\n',
         (unsigned int) &p_i_variable,
         (unsigned int) p_i_variable);
  printf('i_variable with value %d has address 0x%X \n', i_variable, (unsigned int) &i_variable);
  printf('Obtain value of i_variable using pointer p_i_variable: %d\n', *p_i_variable);

  return 0;
}

This would show:

i_variable with value 22 has address 0xBF982BC8
p_i_variable (P.O Box with address 0xBF982BC4) is pointing to address 0xBF982BC8
i_variable with value 50 has address 0xBF982BC8
p_i_variable (P.O Box with address 0xBF982BC4) is pointing to address 0xBF982BC8
i_variable with value 100 has address 0xBF982BC8
Obtain value of i_variable using pointer p_i_variable: 100

One of the things you must have in consideration is the case of dangling pointers. A dangling pointer is a pointer that points to an invalid direction in the memory.

If we create a pointer variable for example:

/* This is a dangling pointer */
int *pointer_integer;

int integer;

/* Now pointer_integer is not a dangling pointer anymore */
pointer_integer = &integer;

and right away we try to read from it, the value inside the variable can be any value, it could be pointing to any direction in memory. If we try to write anything to that direction we could create a segmentation fault.

So if we are not going to use this variable right away, it is a good idea to instantiated with NULL (which have a value of 0):

/* this is not a dangling pointer */
int *pointer_integer = NULL;

The next post, we are going to talk about pointers with arrays, and double-pointers.

Share