Pointers - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Pointers

Description:

Hand Power Drill = Hospital Visit. Pointers carelessness = Core Dump ... a function to sort them such that i has the smaller, and j has the larger value... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • The best (and worst)
  • thing about C

2
Best?
  • Very Powerful
  • Needed for any complex data structures
  • Needed for dynamic memory management
  • The Sharpest tool in your C toolkit.
  • Makes a lot of things easier
  • Faster, Smaller, etc

3
Worst?
  • If used incorrectly it can be bad.
  • Hand Power Drill Hospital Visit
  • Pointers carelessness Core Dump
  • Segmentation faults are common when you misuse
    pointers.
  • But, errors dont always crash sometimes they
    mess upyour program later

4
What is a pointer anyway?
  • It is a type (int, float, pointer to int..)
  • There is a pointer type for every type
  • A pointer to an int int
  • A pointer to a float float
  • They can even point to other pointers!
  • A pointer to a pointer to an int int
  • A pointer to a pointer to a pointer to a pointer
    to an int int
  • Etc.. (You usually see 1 star occasionally 2
    Almost never more than that though.)

5
Figure 9-12
Declaring and Initializing
Declare and initialize in one step
6
Figure 9-5
Yeah, but what is a pointer?
int a -123 int p a
7
Yeah, but what is a pointer?
  • Its a variable that points to another.
  • int val / regular int /
  • int ptr / pointer to an int /

val
ptr
int
int
8
Yeah, but what is it?
  • Its a variable that points to another.
  • int val / regular int /
  • int ptr / pointer to an int /
  • ptr val / ptr contains the
  • address-of val /
  • / ptr points to val /

val
ptr
int
int
9
Yeah, but what is it?
  • Its a variable that points to another.
  • ptr 42 / the target of ptr
  • becomes 42, whatever
  • that target may be
  • indirection /

val
ptr
42
int
int
10
Figure 9-7
int x, p, q p x q x
11
Figure 9-7
int x, p, q p x q x
12
Figure 9-7
int x, p, q p x q x
13
Figure 9-7
int x, p, q p x q x
14
Figure 9-21
15
And this is useful because
  • Bear with me for a moment
  • Lets say we have a program which has two
    integers (int i, j), and we want to be able to
    call a function to sort them such that i has
    the smaller, and j has the larger value
  • How do we swap values in a function without using
    pointers?

16
And this is useful because
  • Bear with me for a moment
  • Lets say we have a program which has two
    integers (int i, j), and we want to be able to
    call a function to sort them such that i has
    the smaller, and j has the larger value
  • How do we swap values in a function without using
    pointers?
  • WE CANT!!!

17
What about
  • int main()
  • int i2, j5
  • swap(i, j)
  • return 0

i
main
j
2
5
18
What about
  • void swap(int a, int b)
  • int main()
  • int i2, j5
  • swap(i, j) // Pass by reference
  • return 0

a
b
temp
swap
i
main
j
2
5
19
swaps
  • By using pointers we can make ONE more generic
    swap routine
  • Using Pass by reference instead of Pass by
    value
  • void swap(int a, int b) // Swaps any two
    ints!
  • int tmp
  • tmp a
  • a b
  • b tmp

20
How do we use it?
  • void swap(int a, int b)
  • int tmp
  • tmp a
  • a b
  • b tmp

array1

12
46
7
64
9
10
87
0
1
2
3
4
5
6
a
b
tmp
swap
i
main
j
swap( i, j )
2
5
21
How do we use it?
  • void swap(int a, int b)
  • int tmp
  • tmp a
  • a b
  • b tmp

a
b
temp
a
b
tmp
swap
swap
10
2
i
main
j
i
main
j
swap( i, j )
2
5
2
5
22
How do we use it?
  • void swap(int a, int b)
  • int tmp
  • tmp a
  • a b
  • b tmp

a
b
temp
a
b
temp
a
b
tmp
swap
swap
swap
10
10
2
i
main
j
i
main
j
i
main
j
swap( i, j )
2
5
2
5
5
5
23
How do we use it?
  • void swap(int a, int b)
  • int tmp
  • tmp a
  • a b
  • b tmp

a
b
temp
a
b
temp
a
b
temp
a
b
tmp
swap
swap
swap
swap
10
10
10
2
i
main
j
i
main
j
i
main
j
i
main
j
swap( i, j )
2
5
2
5
2
5
5
2
24
When do we need Pointers?
  • When passing an array, it automatically passes by
    reference so anything we do in the function
    changes the original no need to send the
    address for changing elements around
  • What happens when we pass a struct to a function?
  • Recall struct is passed by value we get a copy
    of the struct

25
When do we need Pointers?
  • What happens when we pass a struct to a function?
  • Pass by value, get a duplicate
  • Instead, pass a pointer to it
  • typedef struct
  • int x, y
  • Pt
  • void func( Pt p )
  • int main( )
  • Pt mypoint 1, 2
  • printf( BEFORE x d, y d\n, mypoint.x,
    mypoint.y )
  • func( mypoint )
  • printf( BEFORE x d, y d\n, mypoint.x,
    mypoint.y )
  • return 0

Must wrap p inside ( )
26
Nuthin much about NULL
27
NULL
NULL
  • NULL is a pointer to NOTHING!
  • ptr NULL // NULL is a pointer to
  • // address 0 (on most compilers)
  • What is in ptr?

ptr
int
28
Some pointer secrets
  • pointer int gives you a pointer
  • It moves the pointer forward
  • How much depends on the type of the pointer
  • Example
  • int array5, ptr
  • ptr array2
  • ptr ptr 2

The thing ptr points to is 4 bytes So ptr 1
will move you forward 4 bytes in memory. So, ptr
2 will give you a pointer to the int two cells
after ptr.
29
Some pointer secrets
  • pointer - int gives you a pointer
  • It moves the pointer backward
  • pointer - pointer gives you a int
  • Gives you the difference between the memory cells
  • pointer pointer not valid
  • same with / etc

30
Some pointer secrets
  • int i, a100
  • An array IS a pointer!
  • It points to the beginning of the cells
  • All three of these are the same
  • a a a0
  • ai is equivalent to (ai)
  • what is the difference between ai and ia?
  • Is 4a legal?

31
Arrays are pointers
  • include ltstdio.hgt
  • include ltstdlib.hgt

void swap (int a, int b) int temp temp
a a b b temp
void sort(int arr, int len) int p,i for
(p1pltlenp) for (i0iltlen-pi)
if (arri gt arri1) swap(arri,arri
1)
int main() int array1100, array250,
array3500 ... // fill arrays
sort(array1,100) sort(array2,50)
sort(array3,500) ...
32
Arrays are pointers
  • include ltstdio.hgt
  • include ltstdlib.hgt

void swap (int a, int b) int temp temp
a a b b temp
void sort(int arr, int len) int p,i
for(p1pltlenp) for(i0iltlen-pi)
if(arri gt arri1) swap(arri,arri
1)
swap(arri,arri1)
int main() int array1100, array250,
array3500 ... // fill arrays
sort(array1,100) sort(array2,50)
sort(array3,500) ...
33
Strings
  • Strings are char arrays
  • But, arrays are just pointers
  • So a string is just a pointer to char(s)

34
String functions you may want
  • strcpy(char dest, char src)
  • copy a string from src to dest
  • strcat(char dest, char src)
  • copy src onto the end of dest
  • strncpy(char dest, char src, int max)
  • copy up to max chars from src to dest
  • strncat(char dest, char src, int max)
  • copy up to max chars from src onto the end of dest

35
String functions you may want
  • strchr(char s, char c)
  • Returns a pointer to the first occurrence of the
    char c in the string s, or NULL if it isnt
    there!
  • strstr(char haystack, char needle)
  • Returns a pointer to the first occurrence of the
    string needle in the string haystack, or NULL if
    it isnt there!

36
String Funky Funk
  • String Equality
  • Considerchar p abc, q abcif ( p
    q ) printf( Same address!\n )else
    printf( Different addresses\n )
  • What does it print?
  • Depends on your compiler
  • String constants MAY be stored in the same
    location therefore optimized by pointing to the
    same spot
  • But, some compilers will store them separately!

37
Returning Values
38
Returning values
  • Pointers can also be used to return MULTIPLE
    values instead of just one
  • void func(int a, int b, int c)
  • a 1
  • b 4
  • c 9
  • int main()
  • int number, thingy, value
  • func(number, thingy, value)
  • func(number, number, number)
  • return 0

39
Returning values
  • How can you return TWO (or more) values from a
    function?
  • Example Write a function that takes two numbers
    and returns the quotient AND the remainder!

40
Returning values
  • void long_division( int dividend, int divisor,
    int quotientp, int remainderp)
  • quotientp dividend / divisor
  • remainderp dividend divisor
  • Example Write a function that takes two numbers
    and returns the quotient AND the remainder!

Dividend and divisor are input only
Dividend and divisor are input only
41
Returning values
  • void long_division( int dividend, int divisor,
    int quotientp, int remainderp)
  • quotientp dividend / divisor
  • remainderp dividend divisor
  • Example Write a function that takes two numbers
    and returns the quotient AND the remainder!

Values can be returned to calling function using
pointers
Values can be returned to calling function using
pointers
42
Returning values
  • include ltstdio.hgt
  • void long_division(int dividend, int divisor, int
    quotientp, int remainderp)
  • int main( )
  • int quot, rem
  • long_division(40, 3, quot, rem)
  • printf("40 divided by 3 yields quotient d ",
    quot)
  • printf("and remainder d\n", rem)
  • return 0
  • void long_division( int dividend, int divisor,
    int quotientp, int remainderp)
  • quotientp dividend / divisor
  • remainderp dividend divisor

Send the address to the function
43
Ack! POW! Boom!
44
PROBLEMS!
  • Dereferencing a NULL pointer
  • BOOM!
  • Incorrectly applying address and indirection
    operators. Example pt is a pointer
  • pt 45 // invalid attempt to take address of
    a value
  • pt (miles10) // invalid attempt to take
    address of a value
  • pt miles 10 // valid 10 is added to
    address of miles
  • Addresses of pointer constants cannot be taken
  • Int nums25int ptpt nums // invalid
    nums is an address, so no address of the
    address!pt nums // valid

45
PROBLEM! faults
  • Dont dereference a variable that is not a
    pointer!int num 7printf( d, num )
  • Fastest way to a crash
  • ((int ) NULL) 0
Write a Comment
User Comments (0)
About PowerShow.com