Ch 7-pointers

1,468 views

Published on

Pointer
c code

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

No Downloads
Views
Total views
1,468
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
169
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Ch 7-pointers

  1. 1. Chapter-7 Pointers
  2. 2. Pointers <ul><li>A pointer is a variable that holds a memory address. </li></ul><ul><li>This address is the location of another object in memory. </li></ul><ul><li>For example, if one variable contains the address of another variable, the first variable is said to point to the second. </li></ul><ul><li>Basically, a pointer contains an address of another variable. </li></ul><ul><li>Pointers provide an indirect means of accessing or retrieving the data from memory . </li></ul>
  3. 3. Pointers Variable in memory 1000 1001 1002 1003 1004 1005 1006 103 Memory address
  4. 4. The ‘&’ and ‘*’ <ul><li>Consider the declaration int ab=3; </li></ul><ul><li>This declaration tells the compiler </li></ul><ul><li>Reserve space in memory to hold the integer value. </li></ul><ul><li>Associate the name ab with this memory location. </li></ul><ul><li>Store the value 3 at this location. </li></ul>
  5. 5. The ‘&’ and ‘*’ 3 ab 1000 Location name Value at Location Address
  6. 6. <ul><li>Declaring pointer variable </li></ul><ul><li>In C every variable must be declared for its data type. Since pointer variables contain addresses that belong to a separate data type they must be declared as pointers. </li></ul><ul><li>The declaration of pointer variable takes following form: </li></ul><ul><li>data_type *variable_name; </li></ul><ul><li>For example:- int *q; </li></ul><ul><li>here q is pointer variable that can hold the address </li></ul>
  7. 7. The ‘&’ and ‘*’ <ul><li>‘ &’  Address of operator </li></ul><ul><li>‘ *’  Value at address operator. Also called the Indirection Operator. </li></ul><ul><li>‘ &a’  Returns the address of variable a. </li></ul><ul><li>‘ *a’  Returns the value stored in a particular address. </li></ul>
  8. 8. An Example:Pointers <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int j=5; </li></ul><ul><li>printf(“Address of j=%dn”,&j); printf(“Value of j=%dn”,j); </li></ul><ul><li>printf(“Value of j=%dn”,*(&j)); </li></ul><ul><li>} </li></ul>Output: Address of j = 1000 Value of j = 5 Value of j = 5
  9. 9. <ul><li>#include <stdio.h> </li></ul><ul><li>int main() </li></ul><ul><li>{ </li></ul><ul><ul><li>int a = 10, b = 2; </li></ul></ul><ul><ul><li>int *p1, *p2; </li></ul></ul><ul><ul><li>p1 = &a; p2 = &b; </li></ul></ul><ul><ul><li>printf(&quot;%u %u n&quot;, p1, p2); </li></ul></ul><ul><ul><li>printf(&quot;%d %d n&quot;, *p1, *p2); </li></ul></ul><ul><ul><li>return 0; </li></ul></ul><ul><li>} </li></ul>A program that prints out pointers and the values they point to:
  10. 10. Write a program that computes the area and perimeter of a rectangle: #include <stdio.h> void rectangle(int a, int b, int * area, int * perim); int main() { int x, y; int area, perim; printf(&quot;Enter two values separated by space: &quot; ); scanf(&quot;%d %d&quot;, &x, &y); rectangle(x, y, &area, &perim); printf(&quot;Area is %d Perimeter is %dn&quot;, area, perim); return 0; } void rectangle(int a,int b,int * area,int * perim) { *area = a * b; *perim = 2 * (a + b); }
  11. 11. Pointer Expressions <ul><li>Expressions involving pointers can be classified as follows: </li></ul><ul><li>Pointer Assignments </li></ul><ul><li>Pointer Arithmetic </li></ul><ul><li>Pointer Comparison </li></ul><ul><li>Pointer Conversion </li></ul>
  12. 12. Pointer Assignments <ul><li>Consider the following example </li></ul>Here i’s value is 5 and j’s value is i’s address. i 5 1000 1000 2000 j
  13. 13. Pointer Assignments <ul><li>Since, the variable j is containing an address, it is declared as int *j; </li></ul><ul><li>This declaration tells the compiler that j will be used to store the address of an integer value – i.e j points to an integer. </li></ul>
  14. 14. An Example:Pointer Assignments <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int j=3; </li></ul><ul><li>int *k; </li></ul><ul><li>k = &j; </li></ul><ul><li>printf(“Address of j=%d”,&j); </li></ul><ul><li>printf(“Address of j=%d”,k); </li></ul><ul><li>printf(“Address of j=%d”,&k); </li></ul><ul><li>printf(“Value of k=%d”,k); </li></ul><ul><li>printf(“Value of j=%d”,j); </li></ul><ul><li>printf(“Value of j=%d”,*(&j)); </li></ul><ul><li>printf(“Value of j=%d”,*j); </li></ul><ul><li>} </li></ul>Output: Address of j = 1000 Address of j = 1000 Address of k = 2000 Value of k = 1000 Value of j = 3 Value of j = 3 Value of j = error
  15. 15. Pointer Conversions <ul><li>One type of pointer can be converted to another type of pointer. </li></ul><ul><li>Consider the following example: </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>double x = 100.1,y; </li></ul><ul><li>int *p; </li></ul><ul><li>/*The next statement causes p to point to double*/ </li></ul><ul><li>p = (int*)&x; </li></ul><ul><li>y = *p; </li></ul><ul><li>printf(“The value of x is: %f”,y); </li></ul><ul><li>} </li></ul>
  16. 16. Pointers v/s Arrays <ul><li>Consider the following program </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>static char arr[10]=“Embedded”; </li></ul><ul><li>char *s=“Embedded”; </li></ul><ul><li>printf(“%sn”,arr); </li></ul><ul><li>printf(“%sn”,s); </li></ul><ul><li>} </li></ul>Output: Embedded Embedded
  17. 17. Pointers v/s Arrays <ul><li>Consider the following program </li></ul><ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>static char arr[10]=“Embedded”; </li></ul><ul><li>char *s=“Embedded”; </li></ul><ul><li>s++; </li></ul><ul><li>printf(“%sn”,arr); </li></ul><ul><li>printf(“%sn”,s); </li></ul><ul><li>} </li></ul><ul><li>Output : Embedded </li></ul><ul><li> mbedded </li></ul>
  18. 18. Analysis: Pointers v/s Arrays <ul><li>s++ increments s such that it starts pointing to ‘m’ of Embedded and hence would print mbedded as output. </li></ul><ul><li>arr++ would give an error message because the information known to us is the base address and arr++ is attempting to change this base address. </li></ul>
  19. 19. Arrays of Pointers <ul><li>As there are an array of ints, floats, similarly there can be an array of pointers. </li></ul><ul><li>An array of pointers represent a collection of addresses. </li></ul>
  20. 20. An Example: Arrays of Pointers <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int *arr[4]; </li></ul><ul><li>int i=5,j=10,k=15,l=20,m; </li></ul><ul><li>arr[0]=&i; </li></ul><ul><li>arr[1]=&j; </li></ul><ul><li>arr[2]=&k; </li></ul><ul><li>arr[3]=&l; </li></ul><ul><li>for(m=0;m<=3;m++) </li></ul><ul><li>printf(“%d”,*(arr[m])); </li></ul><ul><li>} </li></ul>
  21. 21. Arrays of Pointers 1000 2500 2000 1500 i 5 10 j 20 15 l k 2500 2000 1500 1000 arr[0] arr[3] arr[2] arr[1] 1200 2700 2200 1700
  22. 22. Pointers To Pointers <ul><li>Pointer as we know is a variable which contains an address of another variable. </li></ul><ul><li>A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER . </li></ul>
  23. 23. Double Pointers 3 i 1000 1000 2000 j 2000 3000 k
  24. 24. Double Pointer <ul><li>main() </li></ul><ul><li>{ </li></ul><ul><li>int i=3; </li></ul><ul><li>int *j; </li></ul><ul><li>int **k; </li></ul><ul><li>j=&i; </li></ul><ul><li>k=&j; </li></ul><ul><li>printf(“Address of i is %d%d%dn”,&i,j,*k); </li></ul><ul><li>printf(“Address of j is %d%dn”,&j,k); </li></ul><ul><li>printf(“Address of k is %dn”,&k); </li></ul><ul><li>printf(“Value of i is %d%d%d%dn”,i,*(&i),*j,**k); </li></ul><ul><li>printf(“Value of j is %dn”,j); </li></ul><ul><li>printf(“Value of k is %dn”,k); </li></ul><ul><li>} </li></ul>
  25. 25. Double Pointer OUTPUT: Address of i is 1000 1000 1000 Address of j is 2000 2000 Address of k is 3000 Value of i is 3 3 3 3 Value of j is 1000 Value of k is 2000
  26. 26. Summary <ul><li>A pointer is a variable that holds a memory address. </li></ul><ul><li>Basically, a pointer contains an address of another variable. </li></ul><ul><li>Pointers provide an indirect means of accessing or retrieving the data from memory. </li></ul><ul><li>Arithmetic operations such as addition, multiplication and division on two pointers are not allowed. </li></ul><ul><li>A pointer variable cannot be multiplied/divided by a constant or a variable. </li></ul><ul><li>A pointer which contains an address of another pointer variable can be defined as a pointer to a pointer or DOUBLE POINTER . </li></ul>

×