📄 chapter 9 structures -- valvano.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0056)http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm -->
<HTML><HEAD><TITLE>Chapter 9: Structures -- 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 9: Structures </FONT></B></P>
<P><B><I><FONT face=Helvetica,Arial>What's in Chapter 9?</FONT></I></B></P>
<DIR>
<P><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#DECLARATIONS">Structure
Declarations</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#ACCESS">Accessing
elements of a structure</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#INITIALIZATION">Initialization
of structure data</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#POINTERS">Using
pointers to access structures</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#FUNCTIONS">Passing
structures as parameters to functions</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#EXTENDEDADDRESS">Example
of MC68HC812A4 extended addressing</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#LINKEDLIST">Example
of a Linear Linked List</A><FONT face=Monaco><BR></FONT><A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm#HUFFMAN">Example
of a Huffman Code</A></P></DIR>
<P><FONT face="Times New Roman,Times">A structure is a collection of variables
that share a single name. In an array, each element has the same format. With
structures we specify the types and names of each of the elements or members of
the structure. The individual members of a structure are referenced by their
subname. Therefore, to access data stored in a structure, we must give both the
name of the collection and the name of the element. Structures are one of the
most powerful features of the C language. In the same way that functions allow
us to extend the C language to include new operations, structures provide a
mechanism for extending the data types. With structures we can add new data
types derived from an aggregate of existing types.</FONT></P>
<P><B><I><FONT face=Helvetica,Arial><A name=DECLARATIONS></A>Structure
Declarations</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Like other elements of C programming, the
structure must be declared before it can be used. The declaration specifies the
tagname of the structure and the names and types of the individual members. The
following example has three members: one 16-bit integer and two character
pointers</FONT></P>
<DIR>
<P><CODE>struct theport{<BR> int
mode; // 0 for I/O, 1 for in only -1 for out
only<BR> unsigned char volatile *addr; // pointer to
its address<BR> unsigned char volatile *ddr;}; // pointer
to its direction reg</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The above declaration does not create any
variables or allocate any space. Therefore to use a structure we must define a
global or local variable of this type. The tagname (<B>theport</B>) along with
the keyword <B>struct</B> can be used to define variables of this new data
type:</FONT></P>
<DIR>
<P><CODE>struct theport PortA,PortB,PortC;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The above line defines the three variables
and allocates 6 bytes for each of variable. If you knew you needed just three
copies of structures of this type, you could have defined them as</FONT></P>
<DIR>
<P><CODE>struct theport{<BR> int
mode;<BR> unsigned char volatile
*addr;<BR> unsigned char volatile
*ddr;}PortA,PortB,PortC;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Definitions like the above are hard to
extend, so to improve code reuse we can use <B>typedef</B> to actually create a
new data type (called <B>port</B> in the example below) that behaves
syntactically like <B>char</B> <B>int</B> <B>short</B> etc.</FONT></P>
<DIR>
<P><CODE>struct theport{<BR> int
mode; // 0 for I/O, 1 for in only -1 for out
only<BR> unsigned char volatile *addr; //
address<BR> unsigned char volatile *ddr;}; // direction
reg<BR>typedef struct theport port;<BR>port PortA,PortB,PortC;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Once we have used <B>typedef</B> to create
<B>port</B>, we don't need access to the name <B>theport</B> anymore.
Consequently, some programmers use to following short-cut:</FONT></P>
<DIR>
<P><CODE>typedef struct {<BR> int
mode; // 0 for I/O, 1 for in only -1 for out
only<BR> unsigned char volatile
*addr; //
address<BR> unsigned char volatile *ddr;}port; //
direction reg<BR>port PortA,PortB,PortC;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Similarly, I have also seen the following
approach to creating structures that uses the same structure name as the
<B>typedef</B> name:</FONT></P>
<DIR>
<P><CODE>struct port{<BR> int mode; // 0
for I/O, 1 for in only -1 for out only<BR> unsigned char
volatile *addr; // address<BR> unsigned char
volatile *ddr;}; // direction reg<BR>typedef struct port port;<BR>port
PortA,PortB,PortC;</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">Imagecraft and Hiware support all of the
above methods of declaring and defining structures.</FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=ACCESS></A>Accessing Members of a
Structure</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">We need to specify both the structure name
(name of the variable) and the member name when accessing information stored in
a structure. The following examples show accesses to individual
members:</FONT></P>
<DIR>
<P><CODE>PortB.mode=-1; // 6811 Port B is output
only<BR>PortB.addr=(unsigned char volatile
*)(0x1004);<BR>PortC.mode=0; // 6811 Port C
is input and output<BR>PortC.addr=(unsigned char volatile
*)(0x1003);<BR>PortC.ddr=(unsigned char volatile
*)(0x1007);<BR>(*PortC.ddr)=0; //
specify PortC as inputs<BR>(*PortB.addr)=(*PortC.addr); // copy from
PortC to PortB</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">The syntax can get a little complicated
when a member of a structure is another structure as illustrated in the next
example:</FONT></P>
<DIR>
<P><CODE>struct theline{<BR> int
x1,y1; // starting point<BR> int
x2,y2; // starting point<BR> char
color;}; // color<BR>typedef struct theline line;<BR>struct
thepath{<BR> line L1,L2; // two
lines<BR> char direction;};<BR>typedef struct thepath
path;<BR>path p; //
global<BR>void Setp(void){ line myLine; path
q;<BR> p.L1.x1=5; // black line from 5,6 to
10,12<BR> p.L1.y1=6;<BR> p.L1.x2=10;<BR> p.L1.y2=12;<BR> p.L1.color=255;<BR> p.L2={5,6,10,12,255}; //
black line from 5,6 to
10,12<BR> p.direction=-1;<BR> myLine=p.L1;<BR> q={{0,0,5,6,128},{5,6,-10,6,128},1}; <BR> q=p;<BR>};</CODE></P></DIR>
<ADDRESS>Listing 9-1: Examples of accessing structures</ADDRESS>
<P> </P>
<P><FONT face="Times New Roman,Times">The local variable declaration <B>line
myLine; </B>will allocate 7 bytes on the stack while <B>path q;</B> will
allocate 15 bytes on the stack. In actuality most C compilers in an attempt to
maintain addresses as even numbers will actually allocate 8 and 16 bytes
respectively. In particular, the 6812 executes faster out of external memory if
16 bit accesses occur on even addresses. For example, a 16-bit data access to an
external odd address requires two bus cycles, while a 16-bit data access to an
external even address requires only one bus cycle. There is no particular
odd-address speed penalty for any 6811 address or for 6812 internal addresses
(internal RAM or EEPROM). Notice that the expression <B>p.L1.x1</B> is of the
type <B>int</B>, the term <B>p.L1</B> has the type <B>line</B>, while just <B>p
</B>has the type <B>path</B>. The expression <B>q=p;</B> will copy the entire 15
bytes that constitute the structure from <B>p </B>to <B>q</B>.</FONT></P>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=INITIALIZATION></A>Initialization of
a Structure</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Just like any variable, we can specify the
initial value of a structure at the time of its definition.</FONT></P>
<DIR>
<P><CODE>path thePath={{0,0,5,6,128},{5,6,-10,6,128},1}; <BR>line
thePath={0,0,5,6,128}; <BR>port PortE={1,<BR> (unsigned
char volatile *)(0x100A),<BR> (unsigned char volatile
*)(0)};</CODE></P></DIR>
<P><FONT face="Times New Roman,Times">If we leave part of the initialization
blank it is filled with zeros.</FONT></P>
<DIR>
<P><CODE>path thePath={{0,0,5,6,128},}; <BR>line
thePath={5,6,10,12,}; <BR>port PortE={1, (unsigned char volatile
*)(0x100A),};</CODE></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -