Pointers by Jumail Bin Taliba Faculty of Computer
Pointers by Jumail Bin Taliba Faculty of Computer Science & Information System Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1
Today’s Topics • Concept of Variables • Concept of Pointers • Declaring Pointer Variables • Manipulating Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 2
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 Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 3
Variables • A char variable uses one byte Print the addresses Output: 142300 142301 Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 4
Variables • An integer variable uses four bytes. • The address of an integer variable is the first byte. Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 5
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. Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 6
Pointers Example: a is a data variable p is a pointer variable that stores the address of a – that is &a. Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 7
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; int *p; p = &a; Pointers| SCP 1103 Programming Technique C | // a is a data variable // p is pointer variable // p then holds the address of variable a Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 8
Example: p and q point to the same variable. int a=-123; int *p; int *q; p=&a; q=p; Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 9
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; q=p; Pointers| SCP 1103 Programming Technique C // this is OK // this is wrong, type mismatch // this is also wrong | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 10
Multiple Declarations • To declare multiple pointers in a statement, use the asterisk for each pointer variable Example: int *p 1, p 2; Only p 1 is a pointer variable; p 2 is an ordinary variable • You may also use typedef to make the declaration clear Example: typedef int *Int. Ptr; Int. Ptr p 1, p 2; Now, both p 1 and p 2 are pointer variables Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 11
Initialization of Pointer Variables Pointer variables can also be declared and initialized in a single statement Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 12
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 Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 13
The & and * Operators void main(void) { int a = 5; int *ptr = &a; printf("%d ", ptr); printf("%d ", &a); Memory Address Prints 2000 printf("%d ", *ptr); 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 printf("%d ", *ptr); } Prints 10 printf("%d ", a); Prints 10 printf("%d ", Pointers| *a); SCP 1103 Programming Technique C | 2000 5 2004 2000 | ptr The addresses are specified by the computer. The addresses above are used only for examples. // This would be an error, because // a is not a pointer variable Jumail, FSKSM, UTM, 2005 a 2008 2009 2010 2011 printf("%d ", &ptr); Prints 2004 *ptr = *ptr + 5; Content Last Updated: September 2006 Slide 14
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 Pointers| SCP 1103 Programming Technique C | // Error! This means &(*x) Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 15
int x; int *p=&x; int *q=&x; Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 16
Caution! Pointer assignments Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 17
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) ); pointer variable p ? dynamic variable – You have to include stdlib. h to use the malloc function Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 18
The malloc Function – The new variable can only be accessed through p. The new variable is then referred to as *p 1 – It can be used anyplace an integer variable can scanf(“%d”, &*p 1 ); // or simply scanf(“%d”, p 1); *p 1 = *p 1 + 7; pointer variable p ? dynamic variable Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 19
Dynamic and Static Variables • 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. Example: static variable int n=99; int *p; p = new int; *p = 10; Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 p 99 static variable n p 10 | dynamic variable Last Updated: September 2006 Slide 20
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 Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 21
The free Function • When dynamic variables are no longer needed, free them to return memory to the freestore Example: Before: free(p); p ? After: p ? The value of p is now undefined and the memory used by the variable that p pointed to is back in the freestore Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 22
Pointers to Pointers Output: 58 Pointers| SCP 1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 23
- Slides: 23