09 1 Pointers


Published on

Published in: Technology, News & Politics
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

09 1 Pointers

  1. 1. Pointers by Jumail Bin Taliba Faculty of Computer Science & Information System
  2. 2. Today’s Topics <ul><li>Concept of Variables </li></ul><ul><li>Concept of Pointers </li></ul><ul><li>Declaring Pointer Variables </li></ul><ul><li>Manipulating Pointers </li></ul>
  3. 3. Variables <ul><li>Variable is a space in memory that is used to hold a value. </li></ul><ul><li>Each variable has a name , content and address </li></ul><ul><li>Variable name is a logical reference, used by the programmer </li></ul><ul><li>Address is a physical reference or actual location , and is used by the computer </li></ul>
  4. 4. Variables <ul><li>A char variable uses one byte </li></ul>Print the addresses Output: 142300 142301
  5. 5. Variables <ul><li>An integer variable uses four bytes. </li></ul><ul><li>The address of an integer variable is the first byte. </li></ul>
  6. 6. Pointer Variables <ul><li>What we have used so far are ordinary variables or also called data variables. </li></ul><ul><li>A data variable contains a value (e.g. integer number, a real number or a character) . </li></ul><ul><li>A pointer variable is another type of variable that contains the address of other variable . It also known as address variable . </li></ul>
  7. 7. Pointers <ul><li>Example: </li></ul><ul><ul><li>a is a data variable </li></ul></ul><ul><ul><li>p is a pointer variable that stores the address of a – that is &a . </li></ul></ul>
  8. 8. Pointer Variable Declaration <ul><li>A pointer variable is declared like the data variable. The difference is, put an asterisk (*) in front of it. </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>int a=-123; // a is a data variable </li></ul></ul><ul><ul><li>int *p; // p is pointer variable </li></ul></ul><ul><ul><li>p = &a; // p then holds the address of variable a </li></ul></ul>
  9. 9. <ul><li>Example: p and q point to the same variable. </li></ul><ul><ul><li>int a=-123; </li></ul></ul><ul><ul><li>int *p; </li></ul></ul><ul><ul><li>int *q; </li></ul></ul><ul><ul><li>p=&a; </li></ul></ul><ul><ul><li>q=p; </li></ul></ul>
  10. 10. <ul><li>The type of a pointer must be matched with the type of the variable that the pointer points to. </li></ul><ul><ul><li>int n; </li></ul></ul><ul><ul><li>int *p; </li></ul></ul><ul><ul><li>double *q; </li></ul></ul><ul><ul><li>p=&n; // this is OK </li></ul></ul><ul><ul><li>q=&n; // this is wrong, type mismatch </li></ul></ul><ul><ul><li>q=p; // this is also wrong </li></ul></ul>
  11. 11. Multiple Declarations <ul><li>To declare multiple pointers in a statement, use the asterisk for each pointer variable </li></ul><ul><ul><li>Example: int *p1, p2; </li></ul></ul><ul><ul><li>Only p1 is a pointer variable; p2 is an ordinary variable </li></ul></ul><ul><li>You may also use typedef to make the declaration clear </li></ul><ul><ul><li>Example: typedef int *IntPtr; </li></ul></ul><ul><ul><li>IntPtr p1, p2; </li></ul></ul><ul><ul><li>Now, both p1 and p2 are pointer variables </li></ul></ul>
  12. 12. Initialization of Pointer Variables Pointer variables can also be declared and initialized in a single statement
  13. 13. Accessing Variables Through Pointer Variables <ul><li>There are two special operators for pointers: address-of operator (&) and indirection operator (*) . </li></ul><ul><li>Address-of operator, & </li></ul><ul><ul><li>is used for getting the address of a variable </li></ul></ul><ul><ul><li>Example: &n </li></ul></ul><ul><ul><ul><ul><li>meaning: “give me the address of variable n” </li></ul></ul></ul></ul><ul><li>Indirection operator (also called dereferencing operator), * </li></ul><ul><ul><li>is used for getting the content of a variable whose address is stored in the pointer. </li></ul></ul><ul><ul><li>Example: *ptr </li></ul></ul><ul><ul><ul><ul><li>meaning: “give me the content of a variable whose address is in pointer ptr” </li></ul></ul></ul></ul><ul><ul><li>Notice that, a pointer declaration also uses an asterisk (*) . Don’t get confused . Pointer declaration and indirection operator are different. </li></ul></ul><ul><ul><li>Indirection operator must only be used with a pointer variable . If n is a data variable, the following would be an error. *n </li></ul></ul>
  14. 14. The & and * Operators <ul><li>void main(void) </li></ul><ul><li>{ int a = 5; </li></ul><ul><li>int *ptr = &a; </li></ul><ul><li>printf(&quot;%d &quot;,ptr); </li></ul><ul><li>printf(&quot;%d &quot;,&a); </li></ul><ul><li>printf(&quot;%d &quot;,&ptr); </li></ul><ul><li>printf(&quot;%d &quot;,*ptr); </li></ul><ul><li>*ptr = *ptr + 5; </li></ul><ul><li>printf(&quot;%d &quot;,*ptr); </li></ul><ul><li>printf(&quot;%d &quot;,a); </li></ul><ul><li>} </li></ul>Memory Address Content 5 2000 2000 2004 a ptr Prints 2000 The addresses are specified by the computer. The addresses above are used only for examples. Prints 2000 Prints 2004 Prints 5. This is how it works. ptr contains 2000 . Go to the address 2000 and get its content => 5. Means that, the value of *ptr is 5. This means, a = a + 5 , because ptr holds the address of a. The new value of a is 10 Prints 10 Prints 10 2008 2009 2010 2011 printf(&quot;%d &quot;, *a ); // This would be an error, because // a is not a pointer variable
  15. 15. <ul><li>The & and * operators are canceling each other </li></ul><ul><li>Example: </li></ul><ul><ul><li>int x; </li></ul></ul><ul><ul><li>int *p=&x; </li></ul></ul><ul><ul><li>*&x is equivalent to x and p. </li></ul></ul><ul><ul><li>However, the following would be an error because x is not a pointer. The * operator can only be used with a pointer variable. </li></ul></ul><ul><ul><ul><ul><li>&*x // Error! This means &(*x) </li></ul></ul></ul></ul>
  16. 16. int x; int *p=&x; int *q=&x;
  17. 17. Caution! Pointer assignments
  18. 18. The malloc Function <ul><li>Using pointers, variables can be manipulated even if there is no identifier for them. This variable is called a dynamic variable (or sometimes nameless variable) </li></ul><ul><ul><li>To create a pointer to a new dynamic variable of type int, use the malloc function </li></ul></ul><ul><ul><ul><li>Example: </li></ul></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><ul><li>int *p; </li></ul></ul></ul><ul><ul><ul><li>p = (int*) malloc ( sizeof(int) ); </li></ul></ul></ul><ul><ul><li>You have to include stdlib.h to use the malloc function </li></ul></ul>p ? dynamic variable pointer variable
  19. 19. The malloc Function <ul><ul><li>The new variable can only be accessed through p . The new variable is then referred to as *p1 </li></ul></ul><ul><ul><li>It can be used anyplace an integer variable can </li></ul></ul><ul><ul><ul><li>scanf(“%d”,&*p1 ); // or simply scanf(“%d”, p1 ); </li></ul></ul></ul><ul><ul><ul><li>*p1 = *p1 + 7; </li></ul></ul></ul>p ? dynamic variable pointer variable
  20. 20. <ul><li>Dynamic variables are variables that are created and destroyed while the program is running. </li></ul><ul><li>Static variables (sometimes called automatic variables ) are variables that are automatically created and destroyed by the computer. </li></ul>Dynamic and Static Variables <ul><ul><ul><li>Example: </li></ul></ul></ul><ul><ul><ul><ul><li>int n=99; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>int *p; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>p = new int; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>*p = 10; </li></ul></ul></ul></ul>p 10 dynamic variable static variable 99 static variable p n
  21. 21. Basic Memory Management <ul><li>An area of memory called the freestore is reserved for dynamic variables </li></ul><ul><ul><li>New dynamic variables use memory in the freestore </li></ul></ul><ul><ul><li>If all of the freestore is used, calls to new will fail </li></ul></ul><ul><li>Unneeded memory can be recycled </li></ul><ul><ul><li>When variables are no longer needed, they can be deleted and the memory they used is returned to the freestore </li></ul></ul>
  22. 22. The free Function <ul><li>When dynamic variables are no longer needed, free them to return memory to the freestore </li></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><li>The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore </li></ul></ul>free(p); <ul><ul><li>After: </li></ul></ul><ul><ul><li>Before: </li></ul></ul>? p p ?
  23. 23. Pointers to Pointers Output: 58 Output: 58 Output: 58