Lect 9(pointers) Zaheer Abbas

730 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
730
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
54
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lect 9(pointers) Zaheer Abbas

  1. 1. Created by Zaheer Abbas Aghani
  2. 2. Data structure lecture 8-9 Pointers
  3. 3. Pointer <ul><li>A Pointer provides a way of accessing a variable (or a more complex kind of data, such as an array) without referring to the variable directly. This mechanism used for this is the address of variable. </li></ul>
  4. 4. What is pointer? <ul><li>A pointer is a variable which declared as a pointer type. It contains the memory address (locations) of another variable. It doesn’t take the value of the variable. </li></ul><ul><li>OR </li></ul><ul><li>Pointer is a variable which contains the address in memory of another variable. </li></ul>
  5. 5. example <ul><li>For example: if one variable is a data type and second variable is the pointer type which points to the first variable, then the contents of second variable is the address of first variable. </li></ul>5 2000 Value at address Address of First variable 2000 3000 First Variable Second Variable
  6. 6. Uses of pointers <ul><li>Pointers provides a way of accessing a variable through memory addresses. </li></ul><ul><li>Use of pointers, assigns memory space and also releases it. This concept helps us in making the best use of the available memory (dynamic memory allocations). </li></ul><ul><li>With pointers, data manipulation is done with address, so the execution time is faster. </li></ul><ul><li>Two-dimensional array and multi-dimensional array representation is easy with pointers. </li></ul><ul><li>Concept of pointers is used in data structure such as linked list. </li></ul>
  7. 7. Declaration of pointers <ul><li>A pointer has to be declared just like any other variable - remember a pointer is just a variable that stores an address. </li></ul><ul><li>When a pointer variable is declared then an asterisk (*) symbols should proceed the variable name. </li></ul><ul><li>Adding an asterisk in front of a variable's name declares it to be a pointer to the declared type. </li></ul>
  8. 8. Declaration of pointers <ul><li>Syntax:- </li></ul><ul><li>int *b; </li></ul><ul><li>Here b is an integer type of pointer. This shows that the pointer variable b stores address of an integer variable. </li></ul><ul><li>The address of any variable is whole number, hence pointer variable also contain whole number. </li></ul>
  9. 9. <ul><li>Similarly we can declare char and floating type pointer:- </li></ul><ul><li>char *p; </li></ul><ul><li>float *q; </li></ul><ul><li>Char *p means ‘p’ contains the address of the variable, which is of char type. </li></ul><ul><li>Float *q means, ‘q’ contains the address of the variable which is of floating type. </li></ul>
  10. 10. Initialization of pointer <ul><li>When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. </li></ul><ul><li>Pointer variable can be initialize by following these steps: </li></ul><ul><li>first declare or initialized an integer variable (suppose int x;) </li></ul><ul><li>declare a pointer variable </li></ul><ul><li>assign address of variable to pointer variable. </li></ul>
  11. 11. <ul><li>syntax </li></ul><ul><li>int a; </li></ul><ul><li>int *b; </li></ul><ul><li>b=&a; </li></ul><ul><li>The & operator represents the address of variable. </li></ul><ul><li>Here b is a variable which contains the address of variable ‘a’. </li></ul>
  12. 12. Dereferencing operation of pointer <ul><li>The contents of any variable is referenced by a pointer variable. For example: if </li></ul><ul><li>int x=5; </li></ul><ul><li>int *b; </li></ul><ul><li>b=&x; </li></ul><ul><li>Now if we want to print the value of x variable by using pointer b. how can it possible? </li></ul><ul><li>It is possible by using * operator with pointer name. </li></ul><ul><li>If we write printf(“%d”,*b) then it print the value of x. </li></ul><ul><li>*b performs the &quot;dereferencing&quot; operation on b; it looks at the address stored in b, and goes to that address and returns the value. </li></ul>
  13. 13. Program <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int a=5; </li></ul><ul><li>int *b; //declare pointer variable. </li></ul><ul><li>b=&a; // assign address of a to pointer variable b. </li></ul><ul><li>printf(“value of a =%d”,a); </li></ul><ul><li>printf(“value of a =%d”,*b); </li></ul><ul><li>printf(“address of a =%u”,b); </li></ul><ul><li>printf(“value of b=address of a =%u”,b); </li></ul><ul><li>} </li></ul>
  14. 14. Pointers arithmetic <ul><li>Arithmetic operation can also be done with pointer variables. </li></ul><ul><li>Postfix, prefix, increment, decrement are possible with pointers. </li></ul><ul><li>In other variables postfix, prefix, increment, decrement means addition or subtraction by 1 with these variables, but here these operations means addition or subtraction of bytes that pointer data type holds, with the value that pointer variable contain. </li></ul>
  15. 15. For example: <ul><li>Example: int a=5; </li></ul><ul><li>int *b; </li></ul><ul><li>b=&a; </li></ul><ul><li>Here b++ or ++b means 1000+2=1002 </li></ul><ul><li>& b– or –b means 1000-2=998. </li></ul><ul><li>Its doesn’t affect the address of ‘a’. </li></ul><ul><li>In other variables </li></ul><ul><li>Ex- int a=5; </li></ul><ul><li>a++ or ++a means 5+1=6 </li></ul><ul><li>and a– or –a means 5-1=4. </li></ul>5 1000 1000 2000 a b 5 2000 1000 b a 1002
  16. 16. Pointers and array <ul><li>Array is the collection of similar data types elements. When we declare an array then consecutive memory locations are allocated to array elements. </li></ul><ul><li>For example: int arr[4]={5,10,15,20) </li></ul><ul><li>Here the base address is the address of 0 th element of array. Since the array is of type int, hence address of next element of array is increment by 2. </li></ul><ul><li>Because the array elements are stored in consecutive memory locations therefore when an array pointer is increment it refers to the next location of its data type. </li></ul>
  17. 17. Example: <ul><li>Let us take an example- </li></ul><ul><li>int arr[4]={5,10,15,20} </li></ul><ul><li>int *a; </li></ul><ul><li>a=&arr[0] </li></ul><ul><li>This shows ‘a’ is a pointer which holds the base address of the array arr or we can write as </li></ul><ul><li>a=arr; </li></ul><ul><li>Now when pointer variable is increment by 1 then it contains the base address+2 b/c pointer is of type int. </li></ul>
  18. 18. Program: <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>int arr[4]={5,10,15,20}; </li></ul><ul><li>int i=0; </li></ul><ul><li>int *b; </li></ul><ul><li>b=arr // we can also write b=&arr[0] </li></ul><ul><li>for(i=0;i<4;i++) </li></ul><ul><li>{ </li></ul><ul><li>printf(“value of arr[%d]=%dn”,i,*b); </li></ul><ul><li>printf(“address of arr[%d]=%un”,i, b); </li></ul><ul><li>b=b+1; </li></ul><ul><li>} </li></ul>

×