Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer.

Similar presentations


Presentation on theme: "Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer."— Presentation transcript:

1 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer Science & Information System

2 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 2 Today’s Topics Concept of Variables Concept of Pointers Declaring Pointer Variables Manipulating Pointers

3 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 3 Variables Variable is a space in memory that is used to hold a value. Each variable has a name, content and address Variable name is a logical reference, used by the programmer Address is a physical reference or actual location, and is used by the computer

4 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 4 Variables A char variable uses one byte Print the addresses Output: 142300 142301

5 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 5 Variables An integer variable uses four bytes. The address of an integer variable is the first byte.

6 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 6 Pointer Variables What we have used so far are ordinary variables or also called data variables. A data variable contains a value (e.g. integer number, a real number or a character). A pointer variable is another type of variable that contains the address of other variable. It also known as address variable.

7 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 7 Pointers Example: a is a data variable p is a pointer variable that stores the address of a – that is &a.

8 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 8 Pointer Variable Declaration A pointer variable is declared like the data variable. The difference is, put an asterisk (*) in front of it. Example: int a=-123; // a is a data variable int *p; // p is pointer variable p = &a; // p then holds the address of variable a

9 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 9 Example: p and q point to the same variable. int a=-123; int *p; int *q; p=&a; q=p;

10 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 10 The type of a pointer must be matched with the type of the variable that the pointer points to. int n; int *p; double *q; p=&n; // this is OK q=&n; // this is wrong, type mismatch q=p; // this is also wrong

11 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 11 Multiple Declarations To declare multiple pointers in a statement, use the asterisk for each pointer variable Example: int *p1, p2; Only p1 is a pointer variable; p2 is an ordinary variable You may also use typedef to make the declaration clear Example: typedef int *IntPtr; IntPtr p1, p2; Now, both p1 and p2 are pointer variables

12 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 12 Initialization of Pointer Variables Pointer variables can also be declared and initialized in a single statement

13 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 13 Accessing Variables Through Pointer Variables There are two special operators for pointers: address-of operator (&) and indirection operator (*). Address-of operator, & –is used for getting the address of a variable –Example: &n meaning: “give me the address of variable n” Indirection operator (also called dereferencing operator), * –is used for getting the content of a variable whose address is stored in the pointer. –Example: *ptr meaning: “give me the content of a variable whose address is in pointer ptr” –Notice that, a pointer declaration also uses an asterisk (*). Don’t get confused. Pointer declaration and indirection operator are different. –Indirection operator must only be used with a pointer variable. If n is a data variable, the following would be an error. *n

14 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 14 The & and * Operators void main(void) { int a = 5; int *ptr = &a; printf("%d ",ptr); printf("%d ",&a); printf("%d ",&ptr); printf("%d ",*ptr); *ptr = *ptr + 5; printf("%d ",*ptr); printf("%d ",a); } Memory Address Content 5 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 2008 2009 2010 2011 printf("%d ", *a ); // This would be an error, because // a is not a pointer variable

15 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 15 The & and * operators are canceling each other Example: int x; int *p=&x; *&x is equivalent to x and p. However, the following would be an error because x is not a pointer. The * operator can only be used with a pointer variable. &*x // Error! This means &(*x)

16 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 16 int x; int *p=&x; int *q=&x;

17 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 17 Caution! Pointer assignments

18 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 18 The malloc Function 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) –To create a pointer to a new dynamic variable of type int, use the malloc function Example: int *p; p = (int*) malloc ( sizeof(int) ); –You have to include stdlib.h to use the malloc function p ? ? dynamic variable pointer variable

19 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 19 The malloc Function –The new variable can only be accessed through p. The new variable is then referred to as *p1 –It can be used anyplace an integer variable can scanf(“%d”,&*p1 ); // or simply scanf(“%d”, p1 ); *p1 = *p1 + 7; p ? ? dynamic variable pointer variable

20 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 20 Dynamic variables are variables that are created and destroyed while the program is running. Static variables (sometimes called automatic variables) are variables that are automatically created and destroyed by the computer. Dynamic and Static Variables p 10 dynamic variable static variable 99 static variable p n Example: int n=99; int *p; p = new int; *p = 10;

21 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 21 Basic Memory Management An area of memory called the freestore is reserved for dynamic variables –New dynamic variables use memory in the freestore –If all of the freestore is used, calls to new will fail Unneeded memory can be recycled –When variables are no longer needed, they can be deleted and the memory they used is returned to the freestore

22 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 22 The free Function When dynamic variables are no longer needed, free them to return memory to the freestore Example: The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore free(p); ? ? p After: Before: p ? ?

23 Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 23 Pointers to Pointers Output: 58


Download ppt "Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1 Pointers by Jumail Bin Taliba Faculty of Computer."

Similar presentations


Ads by Google