advanced application of pointers - montana state univ application of pointers csci 112: programming...

19
1 Advanced Application of Pointers CSCI 112: Programming in C

Upload: lynhu

Post on 20-May-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

1

Advanced Application of Pointers

CSCI 112: Programming in C

Page 2: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

2

The NULL pointer

2

• If we want to denote that a pointer is “pointing to nothing”, we can set it equal to the NULL constant.

• This constant is defined in stdlib and stdio, as well as some other libraries.• Its value is 0x0, which is hexadecimal for 0.• In other words, setting a pointer to NULL sets it to point to the address 0 in

memory.• What does the NULL definition look like?

Page 3: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

3

Void Pointers

3

• We can have pointers to a specific type: int*, char*, etc.• But sometimes, do we really care what type the pointer points to?• A pointer always just stores an address, regardless of type.• We can use a void* pointer to say, ”this variable points somewhere in

memory, but we don’t know/care to what type of variable”• This is useful if we want to write code that operates on any pointer

regardless of type• We can cast pointers between typed and void:

int *int_pointer;void *void_pointer = (void *) int_pointer;

Page 4: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

4

Pointers to pointers

4

• Yes, this is starting to sound like inception, but it really makes sense that C lets us point a pointer to another pointer

• If we have an int pointer that points to an int, we can have a second pointer that points to the pointer which points to the int.

• Clear as mud? Let’s see them in action.

int x; // An integerint *p; // A pointer to an integerint **pp; // A pointer to an integer pointer

Page 5: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

5

Pointers to pointers

5

int x;int *p; int **pp;

x

0x1

**pp

0x4

*p

0x6

Declare an int, an int pointer, and a pointer to an int pointer. Allocate

space in memory for these variables.

Page 6: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

6

Pointers to pointers

6

int x;int *p; int **pp;p = &x;

x

0x1

0x4

*p

0x6

Set p to point to x.

**pp

Page 7: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

7

Pointers to pointers

7

int x;int *p; int **pp;p = &x;

x

0x1

0x4

*p

0x1

0x6

Set p to point to x.

**pp

Page 8: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

8

Pointers to pointers

8

int x;int *p; int **pp;p = &x;pp = &p;x

0x1

0x4

*p

0x1

0x6

Set pp to point to p.(An int** must be assigned the

address of an int *)

**pp

Page 9: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

9

Pointers to pointers

9

int x;int *p; int **pp;p = &x;pp = &p;x

0x1

0x6

0x4

*p

0x1

0x6

Set pp to point to p.(An int** must be assigned the

address of an int *)

**pp

Page 10: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

10

Pointers to pointers

10

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;

x

0x1

0x6

0x4

*p

0x1

0x6

What does this line do?

**pp

Page 11: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

11

Pointers to pointers

11

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;

x

3

0x1

0x6

0x4

*p

0x1

0x6

Sets x to 3

**pp

Page 12: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

12

Pointers to pointers

12

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;

x

3

0x1

0x6

0x4

*p

0x1

0x6

What does this line do?

**pp

Page 13: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

13

Pointers to pointers

13

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;

x

3

0x1

0x6

0x4

*p

0xC (12)

0x6

Sets p to point to address 0xB (12)Dereferencing pp gives us access to p,

as that is what pp pointed to

0xC

**pp

Page 14: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

14

Pointers to pointers

14

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;*pp = &x;

x

3

0x1

0x6

0x4

*p

0xC (12)

0x6

What does this line do?0xC

**pp

Page 15: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

15

Pointers to pointers

15

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;*pp = &x;

x

3

0x1

0x6

0x4

*p

0x1

0x6

Sets p to point at x again0xC

**pp

Page 16: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

16

Pointers to pointers

16

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;*pp = &x;**pp = 8;

x

3

0x1

0x6

0x4

*p

0x1

0x6

What does this line do?0xC

**pp

Page 17: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

17

Pointers to pointers

17

int x;int *p; int **pp;p = &x;pp = &p;

*p = 3;*pp = 12;*pp = &x;**pp = 8;

x

8

0x1

0x6

0x4

*p

0x1

0x6

Sets x to 8The first * dereferences pp, giving us p.The second * dereferences p, giving us

access to x.

0xC

**pp

Page 18: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

18

2D arrays: a clarification

18

• A 2D array, although it is an array of arrays, is not equivalent to a double pointer.

• The size of the 2…n dimensions are actually part of the type.• The type of int arr1[2][2] is int[2][2], not int**.• However, we can (and do, think char **argv) create 1D arrays of

pointers, where each of the elements is a pointer to another 1D array.• For example, int *arr2[]. In this case, arr2 is of type int**

Page 19: Advanced Application of Pointers - Montana State Univ Application of Pointers CSCI 112: Programming in C. 2 The NULL pointer 2 • If we want to denote that a pointer is “pointing

19

2D arrays: a clarification

19

• A 2D array, although it is an array of arrays, is not equivalent to a double pointer.

• The size of the 2…n dimensions are actually part of the type.• The type of int arr1[2][2] is int[2][2]