The Basics about Pointers

C++ language offers and amazing thing called pointers. A Pointer provides some extremely interesting programming techniques that are used to perform implement callbacks and set or get a value of variables. Normally there is very little that needs to be done to use a pointer (which is stored in the memory or reverses the addresses in the memory). As a side note a C++ program does not actively pick a location for the memory address. If this is needed then we can insert code at the start of our program such as:

#define MY_REGISTER (*(volatileuint8_t*)0x12345678u)

The u or U is to represent Unsigned

What are pointers?

Pointers are variables, which point to the address in the memory with its values. You must keep in mind that a running program reserves a certain space in the main memory. Because of this, the when a compiled code is executed the variables are put into memory. Thus, this pointer inside program code, like a character filed, is nothing else than address. It is only important how your compiler or processor interrupt those pointers from the memory.

How to – pointers

When you want to call a pointer, there are two methods:

First:  Invoking a variable as an address type the asterisk this is done by using the “*” after its type

Second: Calling a variable by value, type ampersand “*” before its character for invoked as value.

To clarify this, please see the following:

int a = 1; // Declare veriable

int *b = &a; //Declare pointer

cout << "a = " << a << "/n" //print value

cout << "b - address [memory location's address = ] " << b <<"\n"; //print pointer address

cout << "b - actual value = " << *b <<"\n\n"// print pointer

A pointer is nothing more than a variable, it must be defined as usual. In regard to “a” (defined as integer) value equal 1. The pointer b (*b) points to value equal 1 as it copied value from “a”

In the example, the pointer “b” points to value and the address could be printed in the following example:

The output:

Usage pointers on the C++ program

In the following examples there are usages of pointer C++ source code program.
The copy one pointer to another as same exactly as variable in the flowing example:

int a = 15; // to declare a variable integer 
	 int *b = &a; // to declare a variable interger as pointer
	 int *c = b;  // to copy  a variable pointer to pointer

	    
	  cout << "The value of a  is " << a << "\n";  
	  cout << "The value of b is "  <<   *b << "\n";
      cout << "The value of c  is "  <<   *c << "\n\n";

The output:

 

 

 

Using pointer as self

The following will illustrate how to work with get or set operation within function.

 

int a = 15; // to declare a variable integer 
	 int * b = &a; // to declare a variable interger as pointer
	 int * c = b;  // to copy  a variable pointer to pointer

	    
	  cout << "The value of a  is " << a << "\n";  
	  cout << "The value of b is "  <<   *b << "\n";
      cout << "The value of c  is "  <<   *c << "\n\n";
	 
    
	 int a = 15; // to declare a variable integer
     int b = 1;  // to declare a variable integer
	 int *r = &b;  // to copy  a variable pointer to pointer

     cout << "The value of a  is " << a << "\n";  
	 cout << "The value of b is "  <<   b << "\n";
     cout << "The value of r  is "  <<   *r << "\n\n";
	 
	 *r=9; // assign the value of b to 9 using r pointer.

	 cout << "The value of a  is " << a << "\n";  
	 cout << "The value of b is "  <<   b << "\n";
     cout << "The value of r  is "  <<   *r << "\n\n";

When this program executed, you should see the following output console application, along with variables used and notice the value of b is overwrite and became 9 when *r=9;

 

 

 

Using pointer as an argument with function:

  1. Using pointer as self – get or set operation: illustrate how to work get or set operation within function.

 

float a = 10; // to declare a variable float
	 int b; // to declare a variable integer

	 bool IsOk =  Add(a , &b);// assign the value of b as an agurment.
      

     cout << "The value of a  is " << a << "\n";  

	 if(IsOk)
	 cout << "The value of b is "  <<   b << "\n\n";

The output

Working with arrays as pointers

the following will illustrate how to work array as pointers.

char * arr [27]; // to declare a variable char pointer ( string ).
     char  barr[27];  
     
	 int ch = 'a'; // start from 'a' char as it here 64

      for ( int I =0; I < 26 ; I++) // for - loop for input character
	  {
	  	  arr[I] = &barr[I]; // assing an array by its Index.  
            
		  barr[I]= (char) I + ch; // convert integer value to char value 
		                          then added one by one char. 
	  }

    for ( int I =0; I < 26 ; I++) // loop by add one as sequence
	  cout <<  *arr [I]<< " "; // print the arrray.

      cout <<endl<<endl; // add two lines at the end of the code.

The output:


Leave a Reply

Your email address will not be published. Required fields are marked *