Title: Pointers
1Pointers
2Pointers and Address
- Pointer variables
- Normal variables contain a specific value (direct
reference). ex count - Pointers contain address of a variable that has a
specific value (indirect reference). Ex countPtr
3Pointers and Address
- Pointer declarations
- used with pointer variables
- int myPtr (pointer to an int )
- Multiple pointers require using a before each
variable declaration - int myPtr1, myPtr2
- Initialize pointers to 0, NULL, or an address
- 0 or NULL points to nothing (NULL preferred)
4Pointers and Address
- Two important operators( and )
- (address operator)
- returns address of the variable
- int y 5
- int yPtr
- yPtr y // yPtr gets address of y
- yPtr points to y
yptr
500000
600000
5Pointers and Address
- (indirection/dereferencing operator)
- Returns a synonym/alias of what its operand
points to - yptr returns y (because yptr y)
- can be used for assignment
- yptr 7 (the same as y 7)
- and are inverses
- y ? y, yptr ? yptr
6Pointer and Function Arguments
- Call by value
- void swap(int x, int y)
-
- int t
- tx
- x y
- y t
-
- a 10, b 20
- swap(a, b)
main
swap
t
a
10
x
10
b
copy
20
y
20
swap cant work correctly
7- Call by reference
- void swap(int x, int y)
-
- int t
- tx
- x y
- y t
-
- a 10, b 20
- swap(a, b)
main
swap
copy the address
t
a
10
x
a
b
20
y
b
x ? the alias of a y ? the alias of b
8Pointer Arithmetic
- Arithmetic operations can be performed on
pointers - Increment/decrement pointer ( or --)
- note /-- has higher precedence than
- (i) ! i
- i ? i, i i 1 (i) ? increase the i
- Add an integer to a pointer( or , - or -)
- Pointer comparison ( lt, , gt )
- See which pointer points to the higher numbered
array element - Also, see if a pointer points to 0(NULL)
- if (ptr NULL)
9Pointer Arithmetic
- Pointers of the same type can be assigned to each
other - If not the same type, a cast operator must be
used - int a (int )b, where b is a type of void
- Exception pointer to void (type void )
- Generic pointer, represents any type
- No casting needed to convert a pointer to void
pointer - void pointers cannot be dereferenced
10Pointers and Arrays
- Arrays and pointers closely related
- Array name like a constant pointer
- int v5 (v can be seen as int )
- int vPtr v (vPtr v0)
- Pointers can do array subscripting operations
- vi vPtri (vi) (vPtri)
11Pointers and Arrays
- The index can be negative number.
- int a5 4, 3, 2, 1, 0
- int pt a 1 // The same as pt a1
- printf("d\n", pt-1)
- The output is 4
12Character pointers and Functions
- A string is a character array and it is
terminated with the null character \0 - Two kinds of declaration of string
- char aString test
- Char pString test
aSting
Test\0
pSting
Test\0
13Case Study String copy
- Strcpy copy string t to string s
- void strcpy(char s, char t)
-
- int i0
- while((si ti) ! \0)
- i
14Case Study String copy
- Using pointer to perform this task
- void strcpy(char s, char t)
-
- while((s t) ! \0)
-
- s
- t
-
15Case Study String copy
- Because has higher precedence, we can rewrite
as following - void strcpy(char s, char t)
-
- while((s t) ! \0)
-
-
- \0 is equal to the integer 0
- void strcpy(char s, char t)
-
- while (s t)
-
-
16Array of pointers
- Arrays can contain pointers
- Example array of strings
- char suit 4 "Hearts", "Diamonds",
"Clubs", "Spades" - suiti is an pointer, suitij is a char
- suit11 i
17Array of pointers
- Simple example command-line arguments
- int main(int argc, charargv)
- argc number of arguments
- argv a string array contains the arguments
- arguments are separated by blanks.
- argv0 is the name by which the program invoked.
- dir /w ? argv0 dir, argv1 /w
18Array of pointers
main(int argc, char args) int i
for(i1 I lt argc i) printf(ss,
argvi, (i lt (argc -1))? )
printf(\n)
At command line Cgt echo Hello, world. Hello,
world. Cgt
19Pointers to Functions
- Pointer to function
- Contains address of function
- Similar to how array name is address of first
element - Function name is starting address of code that
defines function - Function pointers can be
- Passed to functions
- Stored in arrays
- Assigned to other function pointers
20Pointers to Functions
- Declaration
- Declare directly
- int (foo)(char, int ) means that pointer to a
function which takes a char and an int as
arguments and returns an int - nore compare with int foo(char int )
- Embedded in function declaration
- int foo(char a, int b) ? foo is a type of
- int (foo)(char, int )
21Pointers to Functions
- Call via function pointer
- int (foo)(char, int ) ? foo is a pointer to
function, foo is the function - ?(foo)(char1, i) is used to call the function
int add(int a, int b) return(ab) main()
int (foo)(int, int) foo add printf("d\n",
(foo)(4,5))
The output will be 9
22Pointers to Functions
- An example qsort
- in stdlib.h
- protype
- void qsort(void base, size_t nmemb, size_t
size, int(compare)(const void , const void )) - base pointer of the array
- nmemb array size
- size size of each element in array
- compare a compare function
23Pointers to Functions
- Just define compare function for your purpose,
you can sort anything. - For example, compare function for string, float,
and integer.