Pointer Basic Program in C++

Pointer are used to reference/point to other variables. Their value can be set and read and they basically refer to a variable's memory location. If we are referencing to an Integer, we call it a Pointer to an Integer. Pointers are very useful as they contain critical information about a variable i.e. its address in the memory pool. The value of the Pointer itself is the address of the variable. Sometimes, Pointers are also called as Address Variables













Declaring Pointers

Pointers can be declared by using the * operator. We can declare a Pointer to point to different data types by specifying it in the declaration. See below how to create a Pointer to an Integer:

int *Z ;

We can set the value of Z which will be the referenced memory location. See below:

Z = 1000 ; // The Memory Location pointed by this Pointer is 1000 

Now if there is another variable X whose memory location happens to be 1000, we can change the value of X by changing the value of Z. 

What if we do not know the Memory Location of a variable in advance? We can use the "&" operator to easily find and retrieve the Memory Location of a variable. We shall see this later. For now, let us assume that we know the location of the variable X i.e. 1000. 

When we write *Z, we are referencing to the value of variable at location 1000 which happens to be the value of X. both the statements below are correct and equal:

X = 25 ; // First Statement
*Z = 25 ; //Second Statement

To print the value of the Memory location, we can use two statements as well which are equal as well. We do this by using the "&" operator. 

cout<< &X ; // The value of &X is 1000
cout<< Z ; // The value of Z is also 1000

The Address-of operator i.e. "&" is also used to retrieve the memory location of a variable at runtime. Appending the "&" with the variable name gives us the memory location of a variable by returning an Integer Type value. Say for example, if we have a variable int X. The memory location of X can be retrieved by simply using &X. If we desire to store the address of this variable in another variable Y, we can use:


int X , Y ; // Define two Integers
X = 25 ; // Set the value of X
Y = &X; // Store the location of X in Y

Pointers can also be used to point to Funtions. A simple program to demonstrate the making and use of a pointer that points to a function is shown below. First we create a pointer to an Integer Function by prefixing a '*' with the variable name fp and also suffixing the parameter datatype type within brackets. Then we set the pointer to point to MyFunc(). When printing the pointer value with a parameter, we are actually calling the function MyFunc() with a valid parameter. Thus, setting pointers to point to functions can help us avoid writing function names in between code and therefore replace the function with the pointer completely. 

#include <iostream.h>
#include <conio.h>

int myfunc(int a);

int main()
{
     clrscr();
     int (*fp)(int a); // pointer to int f(int)
     fp = myfunc; // points to myfunc(int)
     cout << fp(5); // calls myfunc(int)
     getch();
     return 0;
}

int myfunc(int a)
{
     return a;
}

No comments:

Post a Comment

Custom Search