Professional Documents
Culture Documents
Pointer
Pointer
Pointer:
A pointer is a variable that represents the location (rather than the value) of a data item, such as a
variable or an array element.
[Additional:] For an IBM pc, the lengths of various data types are as follows:
characters 1 byte
integers 2 bytes
floats 4 bytes
long integers 4 bytes
doubles 8 bytes
Whenever we declare a variable, the system allocates, somewhere in the memory, an appropriate
location to hold the value of the variable. Consider the following statement:
int quantity = 179;
This statement instructs the system to find a location for the integer variable quantity and put the
value 179 in that location. We may represent this below figure:
We may have access to the value 179 by using either the name quantity or the address 5000.
Since memory addresses are simply numbers, they can be assigned to some variables too. Such
variables are called pointer variable. A pointer variable is, therefore, nothing but a variable that
contains an address, which is the location of another variable in memory.
Since the pointer is a variable, its value is also stored in the memory in another location.
Suppose, we assign the address of quantity to a variable p. The link between the variables
p and quantity can be visualized as follows:
Since the value of the variable p is the address of the variable quantity, we may access the value
of quantity by using the value of p and therefore, we can say that pointer can create a
perfect copy of variables.
data-type *ptvar;
where ptvar is the name of the pointer variable, and data-type refers to the data type of
the pointer’s object. An asterisk (*) must precede ptvar which denotes that the variable
ptvar is a pointer variable.
The process of assigning the address of a variable to a pointer variable is known as initialization.
Once a pointer variable is declared, we can use the assignment operator to initialize the variable.
Example:
#include<stdio.h>
int main()
{
int a; // Step 1
int *ptr; // Step 2
a = 10; // Step 3
ptr = &a; // Step 4
return(0);
}
P2 p1 variable
address 1 address 2 value
Here, the pointer variable p2 contains the address of the pointer variable p1, which points to the
location that contains the desired value. This is known as multiple indirections.
A variable that is a pointer to a pointer must be declared using additional indirection operator
symbols in front of the name. Example:
int **p2;
Since * has a lower precedence than [ ] , *m[5] declares m as an array of 5 pointers while
(*m)[5] declares m as a pointer to an array of five elements.
Ans:
An array is a series of elements of the same data type. Pointers can be used to manipulate arrays
rather than using an index. The name of an array points to the first element of the array. If you
declare an array in the following manner:
int marks[5];
Pointer of array:
You can point to the first element of the array using either one of the following pointers:
marks //first element
&marks[0] //first element
you can use the array name to refer to the contents of the array elements like:
*(marks) //value of 1st element
*(marks+1) //value of 2nd element
Call by reference:
In call by reference, the location (address) of actual arguments is passed to formal arguments of
the called function. This means by accessing the addresses of actual arguments we can alter them
within from the called function.
There are two reasons why this is not a good idea and that a programmer need to choose between
call by value and call by reference. The reasons are: side effects and privacy. Unwanted side
effects are usually caused by inadvertently changes that are made to a call by reference
parameter. Also in most cases, you want the data to be private and that someone calling a
function only be able to change if you want it. So it is better to use a call by value by default and
only use call by reference if data changes are expected.