Basic Pointers CSCI 112: Programming in C What the #$@# is a - - PowerPoint PPT Presentation

basic pointers
SMART_READER_LITE
LIVE PREVIEW

Basic Pointers CSCI 112: Programming in C What the #$@# is a - - PowerPoint PPT Presentation

Basic Pointers CSCI 112: Programming in C What the #$@# is a pointer? Pointers are variables Instead of holding values, they hold the address of another variable. They point to where that other variable is stored. Some new syntax A


slide-1
SLIDE 1

Basic Pointers

CSCI 112: Programming in C

slide-2
SLIDE 2

What the #$@# is a pointer?

Pointers are variables Instead of holding values, they hold the address of another variable. They “point” to where that other variable is stored.

slide-3
SLIDE 3

Some new syntax

  • A pointer variable is declared using a *
  • A pointer variable “points” to specific type
  • int pointer, char pointer, etc.
  • A pointer to an int:
  • A pointer to a char:

int *intPointer; char *charPointer;

slide-4
SLIDE 4

What does a pointer do?

  • A pointer variable holds an address of another variable
  • Ex: an int* holds address of an int
  • We don’t assign a value to a pointer—we assign it an address!
slide-5
SLIDE 5

What does a pointer do?

  • A pointer variable holds an address of another variable
  • Ex: an int* holds address of an int
  • We don’t assign a value to a pointer—we assign it an address!
  • Remember the address of operator from scanf? (&)

int myInt; // Declare an int variable int *myIntPointer; // Declare an int pointer variable myIntPointer = &myInt; // Point "myIntPointer" to the memory location of // "myInt"

slide-6
SLIDE 6

0x01 0x02 0xAE 0xAF We start out with a bunch of unallocated memory cells. The hexadecimal numbers above each cell represent the address.

slide-7
SLIDE 7

0x01 0x02 0xAE 0xAF

int x;

x Declare an int variable, and allocate a space in memory for it.

slide-8
SLIDE 8

34

0x01 0x02 0xAE 0xAF

int x; x = 34;

x Assign 34 to x. The value 34 goes into the memory cell for x.

slide-9
SLIDE 9

34

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point;

x Declare an int pointer called point. Allocate space in memory for this variable. *point

slide-10
SLIDE 10

34 0x01

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point; point = &x;

x Assign the address of x to point. We can now think of *point as being a “link” to x *point

slide-11
SLIDE 11

34 0x01

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point; point = &x;

x Assign the address of x to point. We can now think of *point as being a “link” to x *point

*point is “pointing” to where x is stored!

slide-12
SLIDE 12

Pointer operations: the “*” symbol

  • There are three uses for it:
  • Multiplication
  • Indicating that a variable is of a pointer type
  • Unary: “follows the pointer” (often called “dereferencing”)

int x = 6 * 7; int *p = &x; int z = *p;

slide-13
SLIDE 13

Working with pointers

In (1), we assign a value to an int variable x, then “point” the variable point to the address of x.

// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point;

slide-14
SLIDE 14

Working with pointers

In (1), we assign a value to an int variable x, then “point” the variable point to the address of x.

// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point; Quick tip: to print the value of a pointer variable, use the “%p” Quick tip: to print the value of a pointer variable, use the “%p” format specifier! format specifier!

slide-15
SLIDE 15

Working with pointers

In (2), we create another int pointer, point2, and set it equal to point. This means *point and *point2 both “point” to the same place…x!

// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point;

slide-16
SLIDE 16

A picture is worth 436 words

34 0x01

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point; point = &x; int *point2; point2 = point;

x point and point2 both reference the same address/variable (x) *point *point2

slide-17
SLIDE 17

Working with pointers

In (3), we declare a new int variable y, and assign the value “pointed to” by point to it.

// 1 int x = 34; int *point = &x; // 2 int *point2 = point; // 3 int y = *point;

slide-18
SLIDE 18

Wait, what?

  • A pointer variable itself only holds an address (of the variable it points

to)

  • But using the unary operator (*) we can “follow the pointer” and

retrieve the value of the variable being pointed to!

  • So if point is pointing to x, the above snippet is actually assigning the

value of x to y (which is of the int type)

int y = *point;

slide-19
SLIDE 19

A picture is worth 436 words

34 0x01

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point; point = &x; int y;

x point is an int pointer to x We declare the int variable y, and space is allocated in memory for it. *point y

slide-20
SLIDE 20

A picture is worth 436 words

34 34 0x01

0x01 0x02 0xAE 0xAF

int x; x = 34; int *point; point = &x; int y; y = *point;

x We dereference point, and assign its value to y. END RESULT: y == x *point y

slide-21
SLIDE 21

Dereferencing works for assignment, too

int x; int *myPointer; // This next line assigns 34 to the variable myPointer points to // Equivalent to writing "x = 34;" *myPointer = 34;

slide-22
SLIDE 22

Use case: output parameters

  • We’re used to functions returning a value

int x = my_function();

slide-23
SLIDE 23

Use case: output parameters

  • We’re used to functions returning a value
  • We can also pass a pointer as an argument to function, and the

function can write the result into that variable:

int x = my_function(); void increment_int(int *value) { *value = *value + 1; } int x = 6; increment_int(x); // x now equals 7