Pointers in C++

Pointers are usually auxiliary variables which allow us to access the values of other variables indirectly. This can be indicated as p = &i, the ampersand in front of i means the address of i is stored by p and not its content.

Pointers are among C's most difficult thing to master, it enable programs to simulate call-by-reference, and to create and manipulate dynamic data structures, this means; data structures that can grow and shrink at the execution time, e.g trees, linked lists, queues and stack.

A variable use in a program is considered as buckets that are never empty; probably filled with some value either by the programmer or filled  by the operating system  if uninitialised. In pointer is like all variable that must be defined before they can be used. The definition 

 int *p, count;


This means that variable p is of type int *(i.e., a pointer to an integer) it is read "p is a pointer to int" or "p points to an object of type int". The variable count is defined to be an int. The * only applies for the p in our definition.


Therefore to declare two variables as a pointer the * should be use as a prefixed to the name of the variables. i.e., 


int  *p, *count;


Pointers can also be define to point to object of any data type.


Let say we have *p = 10;


The star * there in front of p is an indirection operator that forces the system to first retrieve the contents of p, then access the location whose address has just been retrieved from p, and only afterwards, assign value 10 to the location.


Pointer is different from variable in the sense that with variable it is possible to access the value(s) of variables directly in contrast to a pointer that gives access to the contents of the variable indirectly. This means pointer allows direct access to the address of a variable that hold contents. In this case reference a value through a pointer is called indirection.

Fig 1.1  Shows the values and the addresses stored in memory.


Here we can use pointer to access the address and move vertically on a list of memories to give values to the empty spaces.
Hint: i is considered myCharValue1;
        k is considered myCharValue2;
        m is considered *name3; , in code.

Let examine this in code:

#include <iostream>
#include <string>
using namespace std;
int main()
{
char myCharValue1 = 1;                                   // Declared myCharValue1 and have value named 1'
char myCharValue2;  //char myCharValue3;
char *name1;                                                      // Pointer to character.
char *name2;
char *name3;
name1 = &myCharValue1;                              // Pointer  name1' points to address of myCharValue1'
*name1 = 2;                                                      // Assigned 2 to the variable pointed to by name1.

myCharValue2 = 2 + *name1;
name2 = &myCharValue2;
name3 = &myCharValue1;                                                    //Pointer to another pointer 


printf("Address of myCharValue1 is %p\n", name1);           // Print Address

printf("Value of myCharValue1 is %d\n", *name1);              // 
dereferencing a pointer -prints value

printf("Address of myCharValue2 is %p\n", name2);

printf("Value of myCharValue2 is %d\n", *name2);   //*name2 here means dereferencing a pointer

printf("Address of myCharValue3 is %p\n", name3);

printf("Value of myCharValue3 is %d\n", *name3);

}

Note that name1 prints Address while *name1 will print the value that is stored within the address. This is a very simple illustration that cement the concepts of pointers and variables on how their values and address could be accessed using variable without * to access the address and variable named with * at is front, to access the value.

name3 = &myCharValue1; at this line of code will redirect name3 to point to the same address  name1 pointed to. In this case whatever the address of name1 will also be the address of name3.

Code Output:








NOTE:

It is a good practise to include the letters ptr in pointer variable names to make it clear that the variables are pointers an therefore need to be handled appropriately.
in this case we should declare our definition to be:

int* pPtr , *countPtr;



No comments:

Post a Comment

Note: only a member of this blog may post a comment.

New Post

New style string formatting in Python

In this section, you will learn the usage of the new style formatting. Learn more here . Python 3 introduced a new way to do string formatti...