Вы находитесь на странице: 1из 5

POINTER

What is Pointer?
Pointer is a powerful concept in the C language that provides more
efficiency and flexibility in the program execution. A Pointer is basically
variable that store the address of another variable
When we want to access a variable, the computer will search for that
variable in the memory sequentially, taking so much of time. To overcome
this problem the concept of Pointer is introduced. The Pointer stores the
address of variable which helps to locate the variable in the memory quickly
and efficiently.
N
P
50
735

735

N is a variable that holds the value 50. The address of the n is 735 which is
stored in pointer. The addresses of the Pointer are 831 which is stored in one
register in the processor. When we access the pointer it provides us the
address of variable in the memory which saves the time for searching the
variable in the memory.
Advantages of Pointer:1. Pointers are more efficient in handling array and data table.
2. Pointer can be used to return multi-values from a function.
3. Pointers make string handling easier and save storage place.
4. Pointer allows C language to support Dynamic memory management.
5. Pointers reduce length and complexity of the program.
6. Pointers increase the execution speed and reduce the program execution
time.
7. Pointer provides a efficient tool for handling data structure.
8. Pointer permits reference to function.
Declaration of Pointer:Like simple variable we can also declare the pointer variable. The Data type
of the Pointer variable must be same as the data type of the variable the
address of which we are going to store in the Pointer.
Syntax:Example:-

data type *pointer name;


int n, *p;

To declare the Pointer special operator (*) asterisk sign is used before
the Pointer variable name in the declaration.
Assigning address of the Variable to the Pointer:After declaring the next step is to assign the variable address to the pointer
using the assignment statement.
1

Syntax:-

Pointer name = & Variable name;

Example:-

p=&n;

For assigning the address of the variable special operator address of


(&) is used before the variable name. You can not assign a constant value or
an expression to the Pointer. Pointer can only hold address of a variable.
Pointer Arithmetic :We can perform arithmetic operations using pointer. But in that case a space
must be given between the arithmetic operators ad pointer operators.
Eg.-

int a,b, *p, *q,c;


P=&a;
q=&b;
*p=10;
*q=20;
C=*p* *q;

Note:- *p represent the Value of variable ,address of which is store in printer.


P= Address of variable.

Explain Pointer and one dimensional array:Like a simple variable we can also use the pointer with array. An array
is a collection of elements each having their own address in memory. The
Pointer is assign to the address of first element. To move the pointer to the
next address Pinter value is incremented by 1.
Example:-

int a[5], *p;

500

502

504

506

508

Porgram:void main()
{
int om[10], per[10],*p,*q,n;
p=&om[0];
q=&per[0];
printf(enter marks of 10 students);
for(n=0;n<=9;n++)
{
scanf(%d,&*p);
*q= *p*100/500;
p++;
q++;
}
p=p-10;
q=q-10;
for(n+0;n<=9;n++)
{
printf(%d\t%d\n, *p, *q);
p++;
q++;

}
getch();
}
Note- The pointer is incremented by scale factor like in case of integer
scale factor is 2. If the first address is 500, after incremented the next
address is 502.

Explain Pointer and Structure.

Like other variables we can also assign a Pointer to structure Variable.


After storing the address of structure Variable into the Pointer we can access
the structure member using the Pointer name and member selection
operator()
Syntax:- pointername membername;
We can also use the following notation to access the member.
Syntax:- (*pointername).membername;
Example:- struct std
{
int a;
float b;
} n,*p;

Explain Chain of Pointer or Pointer to Pointer.


When a pointer store the address of another pointer then that Pointer
is called Pointer to pointer. It create a chain of pointers. The pointer that
store the address of another pointer must be declared using additional
indirection operator with the pointer name (**q).
Example:- int n=3,*p,**q;
P=&n;
Q=&p;
The Pointer variable q contains the address of the pointer variable p
which points to the location that contains the desired value. This concept is
also called multi indirections.

Вам также может понравиться