📄 chapter 8 arrays and strings -- valvano.htm
字号:
*p, const char *q);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">Assuming the two pointers are directed at
two null-terminated strings, <B>strcmp</B> will return a negative value if the
string pointed to by<B> p</B>is lexicographically less than the string pointed
to by <B>q</B>. The return value will be zero if they match, and positive if the
string pointed to by <B>p</B>is lexicographically greater than the string
pointed to by <B>q</B>. In general C allows the comparison rule used in
<B>strcoll</B> to depend on the current locale, but in ICC11 and ICC12
<B>strcoll</B> is the same as <B>strcmp</B>. </FONT></P>
<UL>
<P><CODE>char *strcpy(char *dst, const char *src);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">We assume <B>scr</B> points to a
null-terminated string and <B>dst</B> points to a memory buffer large enough to
hold the string. <B>strcpy</B> will copy the string (including the null) pointed
to by <B>src</B>, into the buffer pointed to by pointer <B>dst</B>. The pointer
<B>dst</B> is returned. It is the programmer's responsibility to ensure the
destination buffer is large enough.</FONT></P>
<UL>
<P><CODE>size_t strcspn(const char *p, const char *q);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">The string function <B>strcspn</B> will
compute the length of the maximal initial substring within the string pointed to
by <B>p</B>that has no characters in common with the string pointed to by
<B>q</B>. For example the following call returns the value 5.</FONT></P>
<UL>
<P><CODE>n=strcspn("label: ldaa 10,x ;comment"," ;:*\n\t\l");</CODE></P></UL>
<P><FONT face="Times New Roman,Times">A common application of this routine is
parsing for tokens. The first parameter is a line of text and the second
parameter is a list of delimiters (e.g., space, semicolon, colon, star, return,
tab and linefeed). The function returns the length of the first token (i.e., the
size of <I>label</I>). </FONT></P>
<UL>
<P><CODE>size_t strlen(const char *p);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">The string function <B>strlen</B> returns
the length of the string pointed to by pointer <B>p</B>. The length is the
number of characters in the string not counting the null-termination.</FONT></P>
<UL>
<P><CODE>char *strncat(char *p, const char *q, size_t n);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">This function is similar to <B>strcat</B>.
Assuming the two pointers are directed at two null-terminated strings,
<B>strncat</B> will append a copy of the string pointed to by pointer <B>q</B>,
placing it the end of the string pointed to by pointer <B>p</B>. The
parameter<B> n</B>limits the number of characters, not including the null that
will be copied. The pointer <B>p</B>is returned. It is the programmer's
responsibility to ensure the destination buffer is large enough.</FONT></P>
<UL>
<P><CODE>int strncmp(const char *p, const char *q, size_t n);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">This function is similar to <B>strcmp</B>.
Assuming the two pointers are directed at two null-terminated strings,
<B>strncmp</B> will return a negative value if the string pointed to by<B> p
</B>is lexicographically less than the string pointed to by <B>q</B>. The return
value will be zero if they match, and positive if the string pointed to by
<B>p</B>is lexicographically greater than the string pointed to by <B>q</B>. The
parameter<B> n</B>limits the number of characters, not including the null that
will be compared. For example, the following function call will return a zero
because the first 8 characters are the same:</FONT></P>
<UL>
<P><CODE>n=strncmp("MC68HC11A8","MC68HC11E9",8);</CODE></P></UL>
<P>The following function is similar to <B>strcpy</B>.</P>
<UL>
<P><CODE>char *strncpy(char *dst, const char *src, size_t n);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">We assume <B>scr</B> points to a
null-terminated string and <B>dst</B> points to a memory buffer large enough to
hold the string. <B>strncpy</B> will copy the string (including the null)
pointed to by <B>src</B>, into the buffer pointed to by pointer <B>dst</B>. The
pointer <B>dst</B> is returned. The parameter<B> n</B>limits the number of
characters, not including the null that will be copied. If the size of the
string pointed to by <B>src</B> is equal to or larger than <B>n</B>, then the
null will not be copied into the buffer pointer to by <B>dst</B>. It is the
programmer's responsibility to ensure the destination buffer is large
enough.</FONT></P>
<UL>
<P><CODE>char *strpbrk(const char *p, const char *q);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">This function, <B>strpbrk</B> , is called
<I>pointer to break</I>. The function will search the string pointed to by
<B>p</B>for the first instance of any of the characters in the string pointed to
by <B>q</B>. A pointer to the found character is returned. If the search fails
to find any characters of the string pointed to by <B>q</B>in the string pointed
to by <B>p</B>, then a null pointer is returned. For example the following call
returns a pointer to the colon.</FONT></P>
<UL>
<P><CODE>pt=strpbrk("label: ldaa 10,x ;comment"," ;:*\n\t\l");</CODE></P></UL>
<P><FONT face="Times New Roman,Times">This function, like <B>strcspn</B>, can be
used for parsing tokens. </FONT></P>
<UL>
<P><CODE>char *strrchr(const char *p, int c);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">The function <B>strrchr</B> will search
the string pointed to by <B>p</B>from the right for the first instance of the
character in <B>c</B>. A pointer to the found character is returned. If the
search fails to find any characters with the 8-bit value <B>c</B>in the string
pointed to by <B>p</B>, then a null pointer is returned. For example the
following calls set the <B>pt1</B> to point to the 'a' in <I>label</I> and
<B>pt2</B> to point to the second 'a' in <I>ldaa</I>.</FONT></P>
<UL>
<P><CODE>pt1=strchr("label: ldaa 10,x ;comment",'a');<BR>pt1=strrchr("label:
ldaa 10,x ;comment",'a');</CODE></P></UL>
<P><FONT face="Times New Roman,Times">Notice that <B>strchr</B> searches from
the left while <B>strrchr</B> searches from the right .</FONT></P>
<UL>
<P><CODE>size_t strspn(const char *p, const char *q);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">The function <B>strspn</B> will return the
length of the maximal initial substring in the string pointed to by <B>p</B>that
consists entirely of characters in the string pointed to by <B>q</B>. In the
following example the second string contains the valid set of hexadecimal
digits. The function call will return 6 because there is a valid 6-digit
hexadecimal string at the start of the line.</FONT></P>
<UL>
<P><CODE>n=strspn("A12F05+12BAD*45","01234567890ABCDEF");</CODE></P></UL>
<P> </P>
<UL>
<P><CODE>char *strstr(const char *p, const char *q);</CODE></P></UL>
<P><FONT face="Times New Roman,Times">The function <B>strstr</B> will search the
string pointed to by <B>p</B>from the left for the first instance of the string
pointed to by <B>q</B>. A pointer to the found substring within the first string
is returned. If the search fails to find a match, then a null pointer is
returned. For example the following calls set the <B>pt</B> to point to the 'l'
in <I>ldaa</I>.</FONT></P>
<UL>
<P><CODE>pt=strstr("label: ldaa 10,x ;comment","ldaa");</CODE></P></UL>
<P> </P>
<P><B><I><FONT face=Helvetica,Arial><A name=FIFOQ></A>A FIFO Queue Example using
indices</FONT></I></B></P>
<P><FONT face="Times New Roman,Times">Another method to implement a statically
allocated first-in-first-out FIFO is to use indices instead of <A
href="http://www.ece.utexas.edu/~valvano/embed/chap7/chap7.htm#FIFO">pointers</A>.
This method is necessary for compilers that do not support pointers. The purpose
of this example is to illustrate the use of arrays and indices. Just like the
previous FIFO, this is used for order-preserving temporary storage. The function
<B>PutFifo</B> will enter one 8-bit byte into the queue, and <B>GetFifo</B> will
remove one byte. If you call <B>PutFifo</B> while the FIFO is full (<B>Size</B>
is equal to <B>FifoSize</B>), the routine will return a zero. Otherwise,
<B>PutFifo</B> will save the data in the queue and return a one. The index
<B>PutI</B> specifies where to put the next 8-bit data. The routine
<B>GetFifo</B> actually returns two parameters. The queue status is the regular
function return parameter, while the data removed from the queue is return by
reference. I.e., the calling routine passes in a pointer, and <B>GetFifo</B>
stores the removed data at that address. If you call <B>GetFifo</B> while the
FIFO is empty (<B>Size</B> is equal to<B> </B>zero), the routine will return a
zero. Otherwise, <B>GetFifo</B> will return the oldest data from the queue and
return a one. The index <B>GetI</B> specifies where to get the next 8-bit data.
The following FIFO implementation uses two indices and a counter.</FONT></P>
<DIR>
<P><CODE>/* Index,counter implementation of the FIFO */<BR>#define FifoSize 10 /* Number of 8 bit data in the Fifo */<BR>#define
START_CRITICAL() asm(" tpa\n staa %SaveSP\n sei")<BR>#define END_CRITICAL() asm(
ldaa %SaveSP\n
tap")<BR>unsigned char PutI; /* Index
of where to put next */<BR>unsigned char GetI; /* Index
of where to get next */<BR>unsigned char Size; /* Number currently in the FIFO */<BR> /* FIFO is empty if Size=0 */<BR> /* FIFO is full if Size=FifoSize */<BR>char Fifo[FifoSize]; /* The statically allocated data */<BR>void InitFifo(void) {unsigned
char
SaveSP;<BR> START_CRITICAL(); /* make atomic, entering critical section */<BR> PutI=GetI=Size=0; /* Empty when Size==0 */<BR> asm(" cli"); /* end critical section */<BR>}<BR>int PutFifo (char data) { unsigned
char
SaveSP;<BR> if (Size == FifoSize ) <BR> return(0); /* Failed, fifo was full */<BR> else{ <BR> START_CRITICAL(); /* make atomic, entering critical section */<BR> Size++;<BR> Fifo[PutI++]=data; /* put data into fifo */<BR> if (PutI == FifoSize) PutI = 0; /* Wrap */<BR> END_CRITICAL(); /* end critical section */<BR> return(-1); /* Successful */ <BR> }<BR>}<BR>int GetFifo (char *datapt) { unsigned char
SaveSP;<BR> if (Size == 0 ) <BR> return(0); /* Empty if Size=0 */<BR> else{<BR> START_CRITICAL(); /* make atomic, entering critical section */<BR> *datapt=Fifo[GetI++];<BR> Size--;<BR> if (GetI == FifoSize) GetI = 0;<BR> END_CRITICAL(); /* end critical section */<BR> return(-1); }<BR>}</CODE></P></DIR>
<ADDRESS>Listing 8-7: FIFO implemented with two indices and a counter</ADDRESS>
<P>The START_CRITICAL and END_CRITICAL macros are specific to ICC11/ICC12,
otherwise this example will operate using Hiware.</P>
<P><FONT face="Times New Roman,Times">Go to <A
href="http://www.ece.utexas.edu/~valvano/embed/chap9/chap9.htm">Chapter 9 on
Structures</A> Return to <A
href="http://www.ece.utexas.edu/~valvano/embed/toc1.htm">Table of Contents</A>
</FONT></P></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -