23
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

09 1 Pointers

Embed Size (px)

Citation preview

Page 1: 09 1 Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 1

Pointers

byJumail Bin Taliba

Faculty of Computer Science & Information System

Page 2: 09 1 Pointers

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

Page 3: 09 1 Pointers

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

Page 4: 09 1 Pointers

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

Page 5: 09 1 Pointers

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.

Page 6: 09 1 Pointers

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.

Page 7: 09 1 Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 7

PointersExample:

a is a data variablep is a pointer variable that stores the address of a – that is &a.

Page 8: 09 1 Pointers

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

Page 9: 09 1 Pointers

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;

Page 10: 09 1 Pointers

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

Page 11: 09 1 Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 11

Multiple Declarations

• To declare multiple pointers in a statement, usethe asterisk for each pointer variableExample:

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

Page 12: 09 1 Pointers

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

Page 13: 09 1 Pointers

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

Page 14: 09 1 Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 14

The & and * Operatorsvoid 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);}

MemoryAddress 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

200920102011

printf("%d ", *a); // This would be an error, because // a is not a pointer variable

Page 15: 09 1 Pointers

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)

Page 16: 09 1 Pointers

Pointers| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2006 Slide 16

int x;int *p=&x;int *q=&x;

Page 17: 09 1 Pointers

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

Caution! Pointer assignments

Page 18: 09 1 Pointers

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

Page 19: 09 1 Pointers

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

Page 20: 09 1 Pointers

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

1010 dynamic variable

staticvariable

9999

static variable

p

n

Example:

int n=99;int *p;p = new int;*p = 10;

Page 21: 09 1 Pointers

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 isreserved 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

Page 22: 09 1 Pointers

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

??

Page 23: 09 1 Pointers

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

Pointers to Pointers

Output: 58

Output: 58

Output: 58