Lecture18.ppt

advertisement
C Programming
Lecture 18
Arrays and
Pointer Arithmetic
What is an Array?

An array is a sequence of data
items that are:
• all of the same type
– a sequence of integers, or a sequence of
characters, or a sequence of floats, etc.
• indexible
– you can indicate or select the first
element or second element or ...
• stored contiguously in memory
– each element of the sequence is stored in
memory immediately after the previous
element
Relationship Between
Arrays and Pointers

An array name represents the address of
the beginning of the array in memory.
• When an array is declared, the compiler must
allocate a base address and a sufficient
amount of storage (RAM) to contain all the
elements of the array.
• The base address of the array is the initial
location in memory where the array is
stored.
– It is the address of the first element
(index 0) of the array.
Similarities of
Arrays and Pointers

Suppose we write the declaration
#define
int

N
100
ary[N], *p;
and the system causes memory bytes
300, 304, 308, . . ., 696
to be the address of
ary[0], ary[1], ary[2], . . ., ary[99]
with location 300 being the base address.
Then
The two statements
p = ary;
and
p = &ary[0];
are equivalent.
Pointer Arithmetic


Continuing with the previous example:
p = ary + 1;
and
p = &ary[1];
are equivalent and would assign 304
(the address of the 2nd integer in the
array) to p.
Assuming the elements of ary have been
assigned values, we could use the
following code to sum the elements.
sum = 0;
for (p = ary; p < &ary[N]; ++p)
sum += *p;
Three Ways to Sum the Elements
of the Previous Array
int ary[N], *p;
. . .
sum = 0;
for (p = ary; p < &ary[N]; ++p)
sum += *p;
sum = 0;
for (i = 0; i < N; ++i)
sum += *(ary + i);
sum = 0;
for (i = 0; i < N; ++i)
sum += ary[i];
Differences Between
Arrays and Pointers


In many ways arrays and pointers can be
treated alike.
But there is one essential difference:
• Because the array name represents a
constant pointer and not a variable,
you cannot make an assignment to an
array name or perform any operation
that attempts to change the base
address of the array.
ary = p;
++ary;
ary += 2
are all illegal expressions.
Pointer Arithmetic and Element Size


If the variable p is a pointer to a
particular type, then p + 1 yields the
correct machine address for storing and
accessing the next variable of that
type.
• Expressions such as
p + i
and
++p
and
p+= i
also are also valid and make sense.
This means that pointer arithmetic does
not behave exactly the same as other
arithmetic expressions.
Example of Difference Between Expressions
Using Pointer Arithmetic and the Usual
Arithmetic Expressions
int main(void)
{
double ary[2], *p, *q;
p = &ary[0];
q = p + 1;
/* points at base address of ary */
/* equivalent to q = &ary[1]; */
printf(“%d\n”, q - p);
/* 1 is printed */
printf(“%d\n”, (int) q - (int) p); /* 8 is printed */
return(0);
}
Note:
What is printed by the last statement is systemdependent. If a double is stored in 8 bytes
on the machine where the code is being executed,
then an 8 will be printed.
Passing Arrays to Functions

In a function definition, a formal
parameter that is declared as an
array is actually a pointer.
• When an array is passed, its base
address is passed call-by-value.
• The array elements themselves are not
copied.
• As a notational convenience, the
compiler allows array bracket
notation to be used in declaring
pointers as parameters.
Example of Passing an Array to a Function
int sum(int a[], int n)
{
int
i, s = 0;
/* n is size of the array */
Notice -- no size here. That
was provided in the original
for (i = 0; i < n; ++i) declaration of the array.
s += a[i];
return s;
}
alternative function definition:
int sum(int *a, int n)
{
int
i, s = 0;
/* n is size of the array */
for (i = 0; i < n; ++i)
s += a[i];
return s;
}
Equivalence of int a[]; and int *a;
As part of the header of a
function definition the
declaration
int a[]; is equivalent to int *a;
 Within the body of a function,
these are not equivalent.

• The first will create a constant
pointer (and no storage for an array).
• The second will create a pointer
variable.
Various Ways sum[ ] Might be Called
Invocation
What gets computed and returned
sum(v, 100)
v[0] + v[1] + . . . + v[99]
sum(v, 88)
v[0] + v[1] + . . . + v[87]
sum(&v[7], k - 7)
v[7] + v[8] + . . . + v[k - 1]
sum(v + 7, 2 * k)
v[7] + v[8] + . . . + v[2 * k + 6]
Download