Title: Pointers
1Pointers
- The best (and worst)
- thing about C
2Best?
- 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
3Worst?
- 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
4What 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.)
5Figure 9-12
Declaring and Initializing
Declare and initialize in one step
6Figure 9-5
Yeah, but what is a pointer?
int a -123 int p a
7Yeah, 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
8Yeah, 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
9Yeah, 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
10Figure 9-7
int x, p, q p x q x
11Figure 9-7
int x, p, q p x q x
12Figure 9-7
int x, p, q p x q x
13Figure 9-7
int x, p, q p x q x
14Figure 9-21
15And 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?
16And 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!!!
17What about
- int main()
- int i2, j5
- swap(i, j)
- return 0
i
main
j
2
5
18What 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
19swaps
- 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
-
20How 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
21How 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
22How 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
23How 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
24When 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
25When 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 ( )
26Nuthin much about NULL
27NULL
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
28Some 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.
29Some 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
30Some 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?
31Arrays 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) ...
32Arrays 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) ...
33Strings
- Strings are char arrays
- But, arrays are just pointers
- So a string is just a pointer to char(s)
34String 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
35String 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!
36String 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!
37Returning Values
38Returning 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
39Returning 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!
40Returning 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
41Returning 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
42Returning 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
43Ack! POW! Boom!
44PROBLEMS!
- 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
45PROBLEM! faults
- Dont dereference a variable that is not a
pointer!int num 7printf( d, num ) - Fastest way to a crash
- ((int ) NULL) 0