12
Pointer

Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Embed Size (px)

Citation preview

Page 1: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Pointer

Page 2: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

What is pointer ?

A Pointer is nothing but a variable that contains an address which is a location of another variable in memory.If one variable contains the address of another variable, the first variable is said to point to the second variable.A pointer provides an indirect method of accessing the value of data item.Since a pointer is a variable, its value is also used in the memory in another location. for example:- int a=5; int *p; p=&a;

Page 3: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Benefits of pointer

Pointers are used in situations when passing actual values is difficult or not desired. To return more than one value from a function.They increase the execution speed.The pointer are more efficient in handling the data types .Pointers reduce the length and complexity of a program.

Page 4: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

The use of a pointer array to character string results in saving of data.To allocate memory and access it( Dynamic memory Allocation).Implementing linked lists, trees graphs and many other data structure.

Page 5: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Declaration & initialization of pointer

A pointer declaration consists of a base type, a * and a variable name.General declaration syntax is:

For example:-

Initialization of pointer contains the address of another variable.

Page 6: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Pointer increment & scale factor

Pointer increment means its value is increased by the length of the data type that it points to, and this length is called as scale factor. For example:- int *p1; p1=p1+1; p1=2804 + 2; Sizeof (var_name) function is used to know the no. of bytes need for that variable.

Page 7: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Pointer expression

If p1 and p2 are properly declared and initialized pointers, then the following statements are valid. y= *p1 * *p2; sum= sum + *p x= *p; z= (5 * ( - ( *p2 ) ) ) / ( *p1)

Page 8: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Operations with pointers

Addition and subtraction are the only arithmetic operations that can be performed on pointers. For example:- p1= p1+ 4; p1= p1-2; p3= p1-p2;

Following operations are valid with pointers:- if( p1>p2) { } if( p1==p2) if( P1 != p2) p1++; --p2; sum+= *p2;

Page 9: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Following operations are not valid with pointers:- p3=p1+p2; p3=p1/p2; p3=p1*p2; p1=p1/3;

Page 10: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Pointer with function

In C, every type of variable except register, has an address which can be referenced by using pointer variable.Similarly Pointers can also point to function because C functions have addresses.C function’s address provides another way to invoke the function using pointers.To obtain function’s address, mention only the name of function in printf() function as given in the example.

Page 11: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

How to get address of a function

/*A program to get address of a function */

#include<stdio.h>

void main()

{

void show(); /* usual way of invoking a function */

printf(“ The address of show function is=%u”, show);

}

void show()

{

printf(“ \welcome to HPES!!”)

}

Page 12: Pointer. What is pointer ? A Pointer is nothing but a variable that contains an address which is a location of another variable in memory. If one variable

Uses of pointer to function

Pointers are certainly awkward and off-putting and thus this feature of pointer is used for invoking a functionThere are several possible uses : (a) In writing memory resident program. (b) In writing viruses, or vaccines to remove the viruses. (c) In developing COM/DCOM component (d) In VC++ programming to connect events to function calls.