Title: Chapter 15 - Arrays and Pointers
1Chapter 15 - Arrays and Pointers
2Lab 8 Etch-a-Sketch
- An analog-to-digital converter converts
continuous analog signals to discrete digital
numbers. - Jitter is the physical phenomenon that results
from "noise" associated with a analog signal. - manifests itself by the return values "dancing
around - Eliminate noise and smooth out input data using
- A lowpass filter...
- Oversampling...
- Thresholds...
3Lab 8 Etch-a-Sketch
- Digital equivalent of an analog low pass RC filter
unsigned int lowpass_filter(unsigned int input,
unsigned int delay) // Update filter with
current sample. delay delay - (delay gtgt
FILTER_SHIFT) input // Scale output for
unity gain. return delay gtgt FILTER_SHIFT
4Lab 8 Etch-a-Sketch
- Digital equivalent of an analog low pass RC filter
delay_element Input (delay_element gtgt 4))
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Output Output Output Output Output Output Output Output Output Output Output Output
5Lab 8 Etch-a-Sketch
define FILTER_SHIFT 3 // Parameter K //
initial lowpass filter delay values oldx
ADC_read(LEFT_POT) oldy ADC_read(RIGHT_POT) po
t1_delay (oldx ltlt FILTER_SHIFT) (oldx gtgt
FILTER_SHIFT) pot2_delay (oldy ltlt
FILTER_SHIFT) (oldy gtgt FILTER_SHIFT) while(1)
// pass through low-pass filter x
lowpass_filter(ADC_read(LEFT_POT),
pot1_delay) y lowpass_filter(ADC_read(RIGHT_P
OT), pot2_delay) unsigned int
lowpass_filter(unsigned int input, unsigned int
delay) // Update filter with current
sample. delay input - (delay gtgt
FILTER_SHIFT) // Scale output for unity
gain. return (delay gtgt FILTER_SHIFT)
6Concepts to Learn
- Arrays
- C Strings
- Array Arguments
- Pointers
- Pointer Arithmetic
- Swap Example w/Pointers
- Null Pointers
- Arrays and Pointers
- Pointers to Pointers
- Multi-dimensional Arrays
- Command-line Arguments
- Function Pointers
7Arrays
Arrays
- Allocate a sequence of memory locations.
- For example - table of numbers
- Problems
- What if there are 1000 numbers?
- Write a loop to process each number?
- Use an array
- Declare a sequence of four integers.
- int num4
- num0, num1, num2, num3.
- An array is a sequence of like items.
int num0int num1int num2int num3
8Array Syntax
Arrays
- Like any other variable, arrays must be declared
before they are used. - General form
- type variable-namenumber_of_elements
- The array size must be explicit at compile time
needed to reserve memory space - Array elements individually accessed.
- General form
- variable-nameindex
- Zero based subscripts
- No compile-time or run-time limit checking
9Initialization of Arrays
Arrays
- Elements can be initialized in the same way as
the ordinary variables when they are declared. - type array_namesize list of values
- int number3 0, 0, 0
- Remaining uninitialized elements will be set to
zero automatically. - Array declarations may omit the size.
- int counter 1, 1, 1, 1
- Problems with C initialization of arrays
- No convenient way to initialize selected
elements. - No shortcut to initialize large number of
elements.
10Local Array Example
Arrays
array0 0x0000(SP)
array1 0x0002(SP)
array2 0x0004(SP)
array3 0x0006(SP)
array4 0x0008(SP)
array5 0x000a(SP)
array6 0x000c(SP)
array7 0x000e(SP)
array8 0x0010(SP)
array9 0x0012(SP)
x 0x0014(SP)
SP ?
int main() int array10 int x x
array3 1 array6 5 return 0
main 0x87a2 8031 0016 SUB.W
0x0016,SP 0x87a6 431F MOV.W
1,R15 0x87a8 511F 0006 ADD.W
0x0006(SP),R15 0x87ac 4F81 0014 MOV.W
R15,0x0014(SP) 0x87b0 40B1 0005 000C MOV.W
0x0005,0x000c(SP) 0x87b6 430C CLR.W
R12 0x87b8 5031 0016 ADD.W
0x0016,SP 0x87bc 4130 RET
11Local Array Example
Arrays
array0 0x0000(SP)
array1 0x0002(SP)
array2 0x0004(SP)
array3 0x0006(SP)
array4 0x0008(SP)
array5 0x000a(SP)
array6 0x000c(SP)
array7 0x000e(SP)
array8 0x0010(SP)
array9 0x0012(SP)
x 0x0014(SP)
SP ?
int main() int array10 int x for
(x 0 x lt 10 x) arrayx x
return 0
main 0x8040 8031 0016 SUB.W
0x0016,SP 0x8044 4381 0014 CLR.W
0x0014(SP) 0x8048 90B1 000A 0014 CMP.W
0x000a,0x0014(SP) 0x804e 340D JGE
(CDWLmain2E) CDWLmain2B,
CL1 0x8050 411F 0014 MOV.W
0x0014(SP),R15 0x8054 5F0F RLA.W
R15 0x8056 510F ADD.W
SP,R15 0x8058 419F 0014 0000 MOV.W
0x0014(SP),0x0000(R15) 0x805e 5391 0014
INC.W 0x0014(SP) 0x8062 90B1 000A 0014 CMP.W
0x000a,0x0014(SP) 0x8068 3BF3 JL
(CL1) CL2, CDWLmain2E 0x806a
430C CLR.W R12 0x806c 5031 0016
ADD.W 0x0016,SP 0x8070 4130
RET
12Global Array Example
Arrays
int array10 int x int main() for (x 0
x lt 10 x) arrayx x
return 0
main 0x806a 4382 0214 CLR.W
x 0x806e 90B2 000A 0214 CMP.W
0x000a,x 0x8074 340C JGE
(CDWLmain2E) CDWLmain2B,
CL1 0x8076 421F 0214 MOV.W
x,R15 0x807a 5F0F RLA.W
R15 0x807c 429F 0214 0200 MOV.W
x,0x0200(R15) 0x8082 5392 0214 INC.W
x 0x8086 90B2 000A 0214 CMP.W
0x000a,x 0x808c 3BF4 JL
(CL1) CL2, CDWLmain2E 0x808e
430C CLR.W R12 0x8090 4130
RET
13Array Example
Arrays
array0 0x0000(SP)
array1 0x0002(SP)
array2 0x0004(SP)
array3 0x0006(SP)
array4 0x0008(SP)
array5 0x000a(SP)
array6 0x000c(SP)
array7 0x000e(SP)
array8 0x0010(SP)
array9 0x0012(SP)
x 0x0014(SP)
SP ?
int array10 int x gridx1 gridx 2
0x86aa 411F 0014 MOV.W 0x0014(SP),R15 0x86ae
5F0F RLA.W R15 0x86b0 510F
ADD.W SP,R15 0x86b2 432E MOV.W
2,R14 0x86b4 5F2E ADD.W
_at_R15,R14 0x86b6 411F 0014 MOV.W
0x0014(SP),R15 0x86ba 5F0F RLA.W
R15 0x86bc 532F INCD.W R15 0x86be 510F
ADD.W SP,R15 0x86c0 4E8F 0000 MOV.W
R14,0x0000(R15)
14C Strings
C Strings
- A C string is an array of characters
- char outputString16
- C strings are terminated with a zero byte.
- C strings can be initialized when defined
- char outputString "Text"
- which is the same as
- outputString0 'T' outputString1
'e' outputString2 'x' outputString3
't' outputString4 0 - C has no string operators.
- String functions in ltstring.hgt library
Compiler computes the size of the array (4 1
5 bytes)
15Strings are Arrays
C Strings
int main() char string "\nhello!"
printf("s", string)
0x05e8
0x05ea
0x05ec
0x05ee
0x05f0
0x05f2
0x05f4
string0 0x05f6
string2 0x05f8
string4 0x05fa
string6 0x05fc
0x05fe Return Adr
0x0600
0x61/0x0a
? SP
0x6c/0x65
0x6f/0x6c
0x00/0x21
16Passing Arrays as Arguments
Array Arguments
- C passes parameters to functions by value.
- C passes the address of the 1st element by value.
- define MAX_NUMS 5
- int average(int values)
-
- int i, sum 0
- for (i 0 i lt MAX_NUMS i) sum sum
valuesi return (sum / MAX_NUMS) -
- int main()
-
- int numsMAX_NUMS
- 1, 2, 3, 4, 5
- int mean average(nums)
- return 0
0x05e8
values 0x05ea
i 0x05ec
sum 0x05ee
0x05f0 Return Adr
n0 0x05f2
n1 0x05f4
n2 0x05f6
n3 0x05f8
n4 0x05fa
mean 0x05fc
0x05fe Return Adr
0x0600
0x05f2
SP ?
5
15
1
SP ?
2
3
4
5
3
17Pointers
18Pointers
Pointers
- A pointer is a variable that contains an address.
- With pointers
- functions can indirectly access variables.
- functions can modify the arguments passed by the
caller function. - sophisticated data structures can grow and shrink
at run-time. - Arrays and pointers are closely related.
- Array pointers enable us to conveniently process
groups of data such as vectors, lists, and
strings.
19Swap Function Example
Pointers
int main() int a 3 int b 4
swap(a, b) void swap(int a, int b) int
temp a a b b temp
Stack after call to swap()
0x05ea
0x05ec
0x05ee
0x05f0
0x05f2
0x05f4
0x05f6
0x05f8
a 0x05fa 3
b 0x05fc 4
0x05fe Return Adr
0x0600
4
a
3
b
swap
3
temp
Return Adr
main
20Pointer Variables
Pointers
- Pointer variables contain memory addresses.
- Associated with a pointer variable is the type of
value to which it points. - The asterisk () indicates that the following
identifier is a pointer variable. - The ampersand () returns a pointer (address) to
the following identifier. - Pointer examples
- int ptr
- char cp
- double dp
- int p_ptr ptr
- char strings10
21Syntax for Pointer Operators
Pointers
- A pointer variable is declared with the asterisk
operator () - type var // same - whitespace doesnt
matter type var - Dereferencing any expression returns a value
- var returns contents of the memory location
- pointed to by var
- var returns contents of the memory location
- pointed to by the memory location pointed
- to by var
- 3 returns the contents of memory location 3
- A pointer is created with the reference operator
() - var
- Reference must be applied to a memory object
- 3 is illegal as it would return a pointer to a
constant
22Pointers
Pointers
0x05ea
0x05ec
0x05ee
0x05f0
0x05f2
0x05f4
ptr1 0x05f6
ptr2 0x05f8
i 0x05fa
j 0x05fc
0x05fe Return Adr
0x0600
int ptr1 int ptr2 int i 4 int j ptr1
i ptr2 j // What will these
print? printf("\n04x", ptr1) printf("\n04x",
ptr2) printf("\n04x", ptr1) printf("\n04x",
ptr2) j ptr1 printf("\n04x", j)
4
4
0x05fa
0x05fc
0x0004
??????
0x0004
23Operator Precedence and Associativity
Pointers
OPERATORS ASSOCIATIVITY
( ) -gt . left to right
! -- - (type) sizeof right to left
/ left to right
- left to right
ltlt gtgt left to right
lt lt gt gt left to right
! left to right
left to right
left to right
left to right
left to right
left to right
? left to right
- / ltlt gtgt right to left
, left to right
24Pointer Arithmetic
Pointer Arithmetic
- Address calculations depend on size of elements
- ints are 16-bits or 2 bytes per element.
- e.g., to find 4th element, we add 42 to base
address - If double, we'd have to add 16 (44) to find
address of 4th element. - C does size calculations under the
covers,depending on size of item being pointed
to - double x10
- double y x
- (y 3) 13
Allocates 40 bytes (4 per element)
Same as x3 (base address plus 12)
25Incrementing Pointers
Pointer Arithmetic
- A pointer increments according to its type.
- The unary operators and bind more tightly
than arithmetic operators.
0x05ee
y 0x05f0
a0 0x05f2
a1 0x05f4
a2 0x05f6
a3 0x05f8
a4 0x05fa
ip 0x05fc
0x05fe Return Adr
0x0600
int y 0 int a5 1, 5, 9, 13, 17 int ip
a0 y ip 1 ip 1 y ip y
ip y (ip)
1
5
9
// y0, ip0x05f2 // y a01 y2, ip0x05f2 //
a0 a01 y2, ip0x05f2 // a0
a01 y3, ip0x05f2 // ip ip1 y3,
ip0x05f4 // a1 a11 y5, ip0x05f4
13
17
0x05f2
26ip
Pointer Arithmetic
- Form used by experienced C programmers
// strcpy copy s to d version 1 void
strcpy(char d, char s) while ((d s) !
\0) d s
// strcpy copy s to d version 2 void
strcpy(char d, char s) while ((d s)
! \0)
- The value of s is the character that s pointed
to before s was incremented the postfix does
not change s until after this character has been
fetched.
27Incrementing Pointers
Pointer Arithmetic
- int main()
-
- char cptr
- double fptr
- char buffer10
- double array10
- cptr buffer // cptr buffer0
- fptr array // fptr array0
- printf("\n0x04d, 0x04d", cptr, fptr)
- printf("\n0x04d, 0x04d", cptr, fptr)
- return 0
0x05cc, 0x05d6 0x05cd, 0x05da
28Swap Example Fixed!
Swap Example w/Pointers
- Stack after call to swap()
int main() int a 3 int b 4
swap(a, b) void swap(int a, int b)
int temp a a b b temp
0x05ea
0x05ec
0x05ee
0x05f0
0x05f2
0x05f4
0x05f6
0x05f8
a 0x05fa 3
b 0x05fc 4
0x05fe Return Adr
0x0600
a
0x05fa
0x05fc
b
swap
3
temp
Return Adr
main
29Null Pointers
Null Pointers
- Sometimes we want a pointer that points to
nothing. - Used for invalid pointer error returns
- Used to catch errors
- NULL is a predefined macro that contains a value
that non-null pointer should never hold, usually
NULL0. - int p
- p NULL / p is a null pointer /
30Pointers and Arrays
31Arrays and Pointers
Arrays and Pointers
- An array name is essentially a pointer to the
first element in an array. - Can change the value (contents) of a pointer.
-
- char word10
- char cptr
- cptr word // points to word0
32Arrays and Pointers
Arrays and Pointers
char word10 char cptr cptr word //
points to word0
- Given the previous declarations, each of the
following lines are equal.
cptr word word0 address of
word0 (cptr n) word n wordn
address of wordn cptr word word0
value of word0 (cptr n) (word
n) wordn value of wordn
33Array Pointer Arithmetic
Arrays and Pointers
- Address calculations depend on size of elements
- char x4 add 4 to base address
- int x4 add 8 (24) to base address
- long x4 add 16 (44) to base address
- C does size calculations behind the scenes,
depending on type of pointer (size of item being
pointed to) - long x10 // allocates 40 bytes
- long y x
- (y 3) 13 // same as x3 13
34Common Pitfalls with Arrays
Arrays and Pointers
- Overrun array limits.
- There is no boundary checking in C.
- int array10, i for (i 0 i lt 10 i)
// oops - arrayi 0
- Arrays must be statically declared
- Compiler flags run-time declarations
- void SomeFunction(int num_elements)
- int tempnum_elements // error
-
35Initialization of Pointer Arrays
Arrays and Pointers
- Pointer arrays can be initialized as follows
/ month_name return name of n-th month / char
month_name(int n) static char name
"Illegal month", "January", "February",
"March", "April", "May", "June", "July",
"August", "September", "October",
"November", "December" return ( n lt 1 n
gt 12 ) ? name0 namen
36Pointers and More
37Pointers to Pointers
Pointers to Pointers
- Since pointers are variables themselves, they can
be stored in arrays just as other variables can. - Example
- char linesptr8
38Pointers to Pointers
Pointers to Pointers
- Since pointers are variables themselves, they can
be stored in arrays just as other variables can. - Example
- char linesptr8
defghi?
lmnopqrstuvwxyz?
abc?
39Multi-dimensional Arrays
Multi-dimensional Arrays
- Multi-dimensional arrays declared with multiple
indexes - daytabij / rowcol /
- daytabi,j / WRONG! /
- Array elements are stored by rows
- The rightmost subscript varies the fastest
- Array name points to 1st element
- Multi-dimensional arrays passed to a function
must declare the number of elements for every
subscript except the first - func(int daytab 13)
40Command-line Arguments
Command-line Arguments
- When main is called, it is passed two arguments.
- The first (conventionally called argc, for
argument count) is the number of command-line
arguments the program was invoked with. - The second (argv, for argument vector) is a
pointer to an array of character pointers
(strings) that contain the arguments, one per
string. - By conventions, argv0 points to the name by
which the program was invoked. - By standards, argvargc is a null pointer.
41Command-line Arguments
Command-line Arguments
- By standards, argvargc is a null pointer.
/ echo command-line arguments int main(int argc,
char argv ) while (--argc gt
0) printf("ss", argv, (argc gt 1) ? " "
"") printf("\n") return 0
42Function Pointers
Function Pointers
- In C, a function is not a variable, but it is
possible to define pointers to functions which
can be - assigned,
- placed in arrays,
- passed to functions,
- returned by functions, and so on.
int function1(int a, char s) int
function2(int a, char s) int (f2)(int,
char) f0 function1 f1
function2 (fn)(10, "hello")
43Complicated Declarations
Function Pointers
- C is sometimes castigated for the syntax of
declarations, particularly ones that involve
pointers to functions - int f() // f function returning pointer to
int - int (pf)() // pf pointer to function
returning int - What do these do?
- char argv argv pointer to pointer to char
- int (daytab)13 daytab pointer to array13
of int - int daytab13 daytab array13 of pointer to
int - char ((x()))() x function returning pointer
to array of - pointers to function returning char
- char ((x3)())5 x array3 of pointer to
function returning - pointer to array5 of char
44(No Transcript)