Title: Arrays, Part 2
1Arrays, Part 2 We have already learned how to
work with arrays using subscript
notation. Example float myData 3.5, 4.0,
9.34 myData0 2 printf("myData0 is
3.1f\n", myData0) produces myData0 is 5.5
2Pointers and Arrays Pointers and arrays are
closely linked in C. In fact, the array name
evaluates to the address of the first element in
the array. int data 5, 6, 7 int dataptr
data / notice that we dont use the ampersand
here / int firstptr data0 / we use
here since data0 evaluates to a number
/ printf("dataptr is d, firstptr is d\n",
dataptr, firstptr) produces dataptr is 5,
firstptr is 5
3Pointer Arithmetic Remember that arrays consist
of contiguous memory locations. Therefore, we can
increment (or decrement) the addresses to move
through the array. int data 5, 6, 7 int
i for (i 0 i lt 3 i) printf("the value at
address p is d\n", (data i), (data
i)) produces the value at address 0xbffbb608
is 5 the value at address 0xbffbb60c is 6 the
value at address 0xbffbb610 is 7
4Pointer Arithmetic In the previous example, we
also used pointer arithmetic in the
line printf("the value at address p is d\n",
(data i), (data i)) Since data was
declared to be an array of ints, the expression
(data i) adds i sizeof(int) to the address of
data to get the location of the next int. This
is a reason why we cant mix types, e.g., point
an int type pointer to a variable of type
double.
5Pointer Arithmetic We can use pointer arithmetic
with pointers to non-array type variables as
well. int some int someptr some int data
5, 6, 7 int dataptr data printf("pointer
address p, next address p\n", someptr,
someptr 1) printf("pointer address p, next
address p\n", dataptr, dataptr
1) produces pointer address 0x7fffffdb488c,
next address 0x7fffffdb4890 pointer address
0x7fffffdb4880, next address 0x7fffffdb4884
6Pointer Arithmetic Pointer arithmetic handles
the task of determining the address of the next
element in the array. char chararray 68,
97, 114, 105, 110 / 1 byte each / int
intarray 10, 11, 12, 13, 14 / 4 bytes
each / int i printf("chararray
intarray\n") printf("-------------------\n") fo
r(i 0 i lt 5 i) printf("p, p\n",
(chararray i), (intarray i)) produces chara
rray intarray ------------------- 0012FF74,
0012FF60 0012FF75, 0012FF64 0012FF76,
0012FF68 0012FF77, 0012FF6C 0012FF78, 0012FF70
7Pointer Arithmetic We can use the increment and
decrement operators with pointer variables as
well. int data 5, 6, 7, 8, 9 int dataptr
data int i for(i 0 i lt 5
i) printf("d ", (dataptr)) printf("\n")
while(dataptr ! data) dataptr-- / these
two statements could have been combined/ printf(
"d ", dataptr) produces 5 6 7 8 9 9 8 7 6 5
8Pointer Arithmetic int data 5, 8, 2, 10,
23 int values 5, 8, 2, 10, 23 int
dataptr data int valptr values int
i for(i 0 i lt 5 i) / here we increment
the address, then get the value at the new
address / printf("d ", (dataptr)) printf("
\ndata0 is d\n", data0) for(i 0 i lt 5
i) printf("d ", (valptr)) / what does
this do? / printf("\nvalues0 is d\n",
values0) produces 8 2 10 23 0 data0 is 5 5
6 7 8 9 values0 is 10
9Arrays and Functions, Part 2 Earlier we learned
how to pass the address of a non-array type
variable, for example int myValue
5 someFunct(myValue) Weve already been
passing the address of the first member of an
array when we did something like this float
data 1, 2, 3 anotherFunct(data)
10Arrays and Functions, Part 2 The subscript-style
of declaring and defining functions used square
brackets, e.g., void somefunction(int
data) The pointer style of defining
functions is this void somefunction(int
data) See example-pointers-array7.c on the
course website.
112D Arrays Recall that when we declare a
two-dimensional array, we can think of it as an
array of arrays. For example, if we have the
following array declaration, int data34 we
can think of this as an array with three members,
each of which is an array of four ints. We can
visualize it like this
122D Arrays The address of the beginning of the
entire array is the same as the address of the
first row of the array, which is the address of
the first element of the first row. However, to
get to the first row we must dereference the
array name and to get the value of the first
element of the first row we must dereference
twice int sales23 1, 2, 3, 9, 10,
11 printf("address of data is p\n",
sales) printf("address of row 0 of data is
p\n", sales) printf("the value of sales00
is d\n", sales) produces address of data is
0x7fffffed8140 address of row 0 of data is
0x7fffffed8140 the value of sales00 is 1
132D Arrays The general form for getting the
address of any element of any row
is (array_name row) column For example,
when we write (data 1) 2, we are saying add
the size of one row to the address of data, get
the address of this, then add the size of two
elements of a row to this.
142D Arrays The general form for getting the value
of any element of any row is ((array_name
row) column) For example, when we write
((data 2) 3), we are saying add the size
of two rows to the address of data, get the
address of this, add the size of three elements
of a row to this, then get the value at this
address. See example-pointers-array-2D-2.c on
the course website.
15Pointers to 2D Arrays We can also create a
pointer to a 2D array. We must keep in mind that
the first element of a 2D array is a 1D array.
Therefore, we must decide if we wish for the
pointer to point to this 1D array or the first
element of this 1D array. int data4 31,
32, 33, 34, 35, 36, 37, 38 int
(ptr1)4 data / points to first 1D array
/ int ptr2 data00 / points to the
element 31 /
16Pointers to 2D Arrays cont. Whats the
difference? The first example, int (ptr1)4
data / points to first 1D array / allows
pointer arithmetic based on row/column values for
accessing the array elements. The second
example, int ptr2 data00 / points to
the element 31 / only allows pointer arithmetic
based on individual ints. See
example-pointers-to-array.c on the course website.
17Arrays of Pointers We can have an array whose
members are pointers, in this example
pointers-to-int. int data3 int i int x
5 int y 89 int z 34 data0 x data1
y data2 z for(i 0 i lt 3
i) printf("d\n", datai) produces 5 89 34
18Arrays of Strings The typical use for arrays of
pointers is not with numeric types, such as int
or double, but with strings, i.e., an array of
chars. int i, j char text4 "this", /
text0 points to this word / "is",
/ text1 points to this word /
"a", / text2 points to this word /
"string" / text3 points to this word
/ for(i 0 i lt 4 i) j 0 while
((texti j) ! \0) / print each character
/ printf("c", (texti j)) / note the
c / printf(" ") produces this is a string
19Arrays of Strings By changing the format
specifier in the printf() statement, we could
have printed the string this way int i char
text4 "this", / text0 points to this
word / "is", / text1 points to
this word / "a", / text2 points
to this word / "string" / text3
points to this word / for(i 0 i lt 4
i) printf("s ", texti) / could have
used (text i) instead. note the s
/ produces this is a string