📄 chapter 8 arrays and strings -- valvano.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm -->
<HTML><HEAD><TITLE>Chapter 8: Arrays and Strings -- Valvano</TITLE>
<META http-equiv=content-type content=text/html;charset=iso-8859-1>
<META content="MSHTML 5.50.3825.1300" name=GENERATOR>
<META
content="StarMax HD:Microsoft Office 98:Templates:Web Pages:Blank Web Page"
name=Template></HEAD>
<BODY vLink=#800080 link=#0000ff>
<P><!--Developing Embedded Software in C using ICC11/ICC12/Hiware by Jonathan W. Valvano--><B><FONT
face=Helvetica,Arial size=4>Chapter 8: Arrays and Strings </FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 8?</FONT></I></B></P>
<DIR>
<P><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#SUBSCRIPTS">Array
Subscripts</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#DECLARATIONS">Array
Declarations</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#REFERENCES">Array
References</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#NAMES">Pointers
and Array Names</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#NEGATIVE">Negative
Subscripts</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#ADDRESSARITHMETIC">Address
Arithmetic</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#STRINGH">String
Functions defined in string.h</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#FIFOQ">Fifo Queue
Example</A></P></DIR>
<P><FONT face="Times New Roman,Times">An array is a collection of like variables
that share a single name. The individual elements of an array are referenced by
appending a <I>subscript</I>, in square brackets, behind the name. The subscript
itself can be any legitimate C expression that yields an integer value, even a
general expression. Therefore, arrays in C may be regarded as collections of
like variables. Although arrays represent one of the simplest data structures,
it has wide-spread usage in embedded systems.</FONT></P>
<P><FONT face="Times New Roman,Times">Strings are similar to arrays with just a
few differences. Usually, the array size is fixed, while strings can have a
variable number of elements. Arrays can contain any data type (<B>char</B>
<B>short</B> <B>int</B> even other arrays) while strings are usually ASCII
characters terminated with a NULL (0) character. In general we allow random
access to individual array elements. On the other hand, we usually process
strings sequentially character by character from start to end. Since these
differences are a matter of semantics rather than specific limitations imposed
by the syntax of the C programming language, the descriptions in this chapter
apply equally to data arrays and character strings. <A
href="http://www.ece.utexas.edu/~valvano/embed/chap3/chap3.htm#STRING">String
literals</A> were discussed earlier in Chapter 3; in this chapter we will define
data structures to hold our strings. In addition, C has a rich set of predefined
functions to manipulate strings.</FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=SUBSCRIPTS></A>Array
Subscripts</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">When an array element is referenced, the
subscript expression designates the desired element by its position in the
<B>data</B>. The first element occupies position zero, the second position one,
and so on. It follows that the last element is subscripted by [N-1] where N is
the number of elements in the array. The statement</FONT></P>
<DIR>
<P><CODE>data[9] = 0;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">for instance, sets the tenth element of
<B>data</B> to zero. The array subscript can be any expression that results in a
16-bit integer. The following for-loop clears 100 elements of the array
<B>data</B> to zero.</FONT></P>
<DIR>
<P><CODE>for(j=0;j<100;j++) data[j] = 0;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">If the array has two dimensions, then two
subscripts are specified when referencing. As programmers we may any assign
logical meaning to the first and second subscripts. For example we could
consider the first subscript as the row and the second as the column. Then, the
statement</FONT></P>
<DIR>
<P><CODE>ThePosition = position[3][5];</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">copies the information from the 4th row
6th column into the variable <B>ThePosition</B>. If the array has three
dimensions, then three subscripts are specified when referencing. Again we may
any assign logical meaning to the various subscripts. For example we could
consider the first subscript as the x coordinate, the second subscript as the y
coordinate and the third subscript as the z coordinate. Then, the
statement</FONT></P>
<DIR>
<P><CODE>humidity[2][3][4]=100;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">sets the humidity at point (2,3,4) to 100.
Array subscripts are treated as signed 16-bit integers. It is the programmer's
responsibility to see that only positive values are produced, since a negative
subscript would refer to some point in memory preceding the array. One must be
particularly careful about assuming what existing either in front of or behind
our arrays in memory. </FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=DECLARATIONS></A>Array
Declarations</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Just like any variable, arrays must be
declared before they can be accessed. The number of elements in an array is
determined by its declaration. Appending a constant expression in square
brackets to a name in a declaration identifies the name as the name of an array
with the number of elements indicated. Multi-dimensional arrays require multiple
sets of brackets. The examples in Listing 8-1 are valid declarations.</FONT></P>
<DIR>
<P><CODE>short data[5]; /* define data, allocate
space for 5 16-bit integers */<BR>char string[20]; /* define
string, allocate space for 20 8-bit characters */<BR>int time,width[6]; /*
define time, width, allocate space for 16-bit characters */<BR>short
xx[10][5]; /* define xx, allocate space for 50 16-bit integers
*/<BR>short pts[5][5][5]; /* define pts, allocate space for 125
16-bit integers */<BR>extern char buffer[]; /* declare buffer as an
external character array */</CODE></P></DIR>
<ADDRESS>Listing 8-1: Example showing a array declarations</ADDRESS>
<P><FONT face="Times New Roman,Times">Notice in the third example that ordinary
variables may be declared together with arrays in the same statement. In fact
array declarations obey the syntax rules of ordinary declarations, as described
in <A href="http://www.ece.utexas.edu/~valvano/embed/chap4/chap4.htm">Chapters
4</A> and <A
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">7</A>, except
that certain names are designated as arrays by the presence of a dimension
expression.</FONT></P>
<P><FONT face="Times New Roman,Times">Notice the size of the external array,<B>
buffer[]</B>, is not given. This leads to an important point about how C deals
with array subscripts. The array dimensions are only used to determine how much
memory to reserve. <B>It is the programmer's responsibility to stay within the
proper bounds. </B>In particular, you must not let the subscript become negative
for above N-1, where N is the size of the array. </FONT></P>
<P><FONT face="Times New Roman,Times">Another situation in which an array's size
need not be specified is when the array elements are given initial values. As we
will see in <A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm">Chapter 9</A>,
the compiler will determine the size of such an array from the number of initial
values.</FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=REFERENCES></A>Array
References</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">In C we may refer to an array in several
ways. Most obviously, we can write subscripted references to array elements, as
we have already seen. C interprets an unsubscripted array name as the address of
the array. In the following example, the first two lines set <B>x</B>to equal
the value of the first element of the array. The third and fourth lines both set
<B>pt</B> equal to the address of the array. <A
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm">Chapter 7</A>
introduced the address operator & that yields the address of an object. This
operator may also be used with array elements. Thus, the expression
<B>&data[3]</B> yields the address of the fourth element. Notice too that
<B>&data[0]</B> and <B>data+0</B> and <B>data</B> are all equivalent. It
should be clear by analogy that &<B>data</B>[3] and <B>data</B>+3 are also
equivalent.</FONT></P>
<DIR>
<P><CODE>short x,*pt,data[5]; /* a variable, a pointer, and an array */<BR>void
Set(void){<BR> x=data[0]; /* set x equal
to the first element of data
*/<B><BR></B> x=*data; /*
set x equal to the first element of data
*/<BR> pt=data; /* set pt to
the address of data */<BR> pt=&data[0]; /* set
pt to the address of data
*/<BR> x=data[3]; /* set x equal to the
fourth element of data */<B><BR></B> x=*(data+3); /*
set x equal to the fourth element of data
*/<BR> pt=data+3; /* set pt to the
address of the fourth element
*/<BR> pt=&data[3]; /* set pt to the address of
the fourth element */<BR>}</CODE></P></DIR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -