Difference between a variable, reference variable, a constant variable and a pointer variable.

Q3. Explain the difference between a variable, reference variable, a constant variable and a pointer variable. Support your answer with examples.

Ans.

Variable

Variables represent named storage locations, whose values can be manipulated during program run. For instance, to store name of a student and marks of a student during a program run, we require storage locations that too named so that these can be distinguished easily. Variables, called as symbolic variables, serve the purpose. The variables are called symbolic variables because these are named locations. For instance, the following statement declares a variable i of the data type int:

int i;

 

Reference Variable

A reference is an alternative name for an object. A reference variable provides an alias for a previously defined variable. A reference declaration consists of a base type, an & (ampersand), a reference variable name equated to a variable name. The general form of declaring a reference variable is

type & ref-var = var-name;

where type is any valid C++ data type, ref-var is the name of reference variable that will point to variable denoted by var-name.

For example, if sum is declared as a reference variable for a variable total, then both sum and total represent the same variable and can be used interchangeably. Following program segment illustrates it:

int total;

int &sum = total;

total = 100;

cout,,”Sum = “<<sum<<”\n”;

cout<<”Total = “<<total<<”\n”;

 

The output of above program segment will be as follows:

Sum = 100

Total = 100

Both the variables refer to the same data object in the memory, thus, print the same value.

 

Constant Variable

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable. Thus, the value of the named constant cannot be altered during the program run. The general form of constant declaration is as follows:

const type name = value;

where const is the keyword that must be used for declaring a constant, type is any valid C++ data type, name is the name of the constant and value is the constant value of the data type type. For instance,

const int upperage = 50;

declares a constant named a upperage of type integer that holds value 50;

a constant must be initialized at the time of declaration. If you give only const in place of const int, it means the same.

 

Pointer

A pointer is a variable that holds a memory address. This address is usually the location of another variable in memory. If one variable contains the address of another variable, the first variable is said to pint to the second.

 

Memory Address       1050         1051            1052               1053          1054 

1053

Content of memory
variable name       A                                               B

 

A points to B, that is, A is B’s pointer

Figure: The concept of pointer

If a variable is going to hold a pointer, it must b declared as such. A pointer declaration consists of a base type, an * (asterisk), and the variable name. The general form of declaring a pointer variable is

type *ptr;

where type is any valid C++ type and ptr is the name of the pointer variables.

For example:

char *a;

declares a pointer to character.

 

The base type of the pointer defines what type of variables the pointer can point to. Technically, any type of pointer can point anywhere in memory. However, all pointer arithmetic is done relative to its base type so it is important to declare the pointer correctly.

Leave a Reply

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