Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Pointer

2 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;

3 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.

4 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.

5 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.

6 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.

7 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)

8 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;

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

10 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.

11 How to get address of a function /*A program to get address of a function */ #include 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!!”) }

12 Uses of pointer to function Pointers are certainly awkward and off-putting and thus this feature of pointer is used for invoking a function There 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.


Download ppt "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."

Similar presentations


Ads by Google