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

Post on 20-May-2018

216 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Advanced Application of Pointers

CSCI 112: Programming in C

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?

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;

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

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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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]

top related