⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chapter 8 arrays and strings -- valvano.htm

📁 介绍了在嵌入式系统中如何用c来设计嵌入式软件
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  *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>&nbsp;</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>&nbsp;</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>/*&nbsp;Index,counter&nbsp;implementation&nbsp;of&nbsp;the&nbsp;FIFO&nbsp;*/<BR>#define&nbsp;FifoSize&nbsp;10&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;of&nbsp;8&nbsp;bit&nbsp;data&nbsp;in&nbsp;the&nbsp;Fifo&nbsp;*/<BR>#define 
START_CRITICAL() asm(" tpa\n staa %SaveSP\n sei")<BR>#define END_CRITICAL() asm( 
ldaa %SaveSP\n 
tap")<BR>unsigned&nbsp;char&nbsp;PutI;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Index 
of&nbsp;where&nbsp;to&nbsp;put&nbsp;next&nbsp;*/<BR>unsigned&nbsp;char&nbsp;GetI;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Index 
of&nbsp;where&nbsp;to&nbsp;get&nbsp;next&nbsp;*/<BR>unsigned&nbsp;char&nbsp;Size;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Number&nbsp;currently&nbsp;in&nbsp;the&nbsp;FIFO&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;FIFO&nbsp;is&nbsp;empty&nbsp;if&nbsp;Size=0&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;FIFO&nbsp;is&nbsp;full&nbsp;&nbsp;if&nbsp;Size=FifoSize&nbsp;*/<BR>char&nbsp;Fifo[FifoSize];&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;statically&nbsp;allocated&nbsp;data&nbsp;*/<BR>void&nbsp;InitFifo(void)&nbsp;{unsigned 
char 
SaveSP;<BR>&nbsp;&nbsp;START_CRITICAL();&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;make&nbsp;atomic,&nbsp;entering&nbsp;critical&nbsp;section&nbsp;*/<BR>&nbsp;&nbsp;PutI=GetI=Size=0;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Empty&nbsp;when&nbsp;Size==0&nbsp;*/<BR>&nbsp;&nbsp;asm("&nbsp;cli");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;end&nbsp;critical&nbsp;section&nbsp;*/<BR>}<BR>int&nbsp;PutFifo&nbsp;(char&nbsp;data)&nbsp;{&nbsp;unsigned 
char 
SaveSP;<BR>&nbsp;&nbsp;&nbsp;if&nbsp;(Size&nbsp;==&nbsp;FifoSize&nbsp;)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Failed,&nbsp;fifo&nbsp;was&nbsp;full&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;else{&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;START_CRITICAL();&nbsp;&nbsp;&nbsp;/*&nbsp;make&nbsp;atomic,&nbsp;entering&nbsp;critical&nbsp;section&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Size++;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Fifo[PutI++]=data;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;&nbsp;put&nbsp;data&nbsp;into&nbsp;fifo&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(PutI&nbsp;==&nbsp;FifoSize)&nbsp;PutI&nbsp;=&nbsp;0;&nbsp;&nbsp;/*&nbsp;Wrap&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_CRITICAL();&nbsp;&nbsp;/*&nbsp;end&nbsp;critical&nbsp;section&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(-1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Successful&nbsp;*/&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<BR>}<BR>int&nbsp;GetFifo&nbsp;(char&nbsp;*datapt)&nbsp;{&nbsp;unsigned char 
SaveSP;<BR>&nbsp;&nbsp;&nbsp;if&nbsp;(Size&nbsp;==&nbsp;0&nbsp;)&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(0);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Empty&nbsp;if&nbsp;Size=0&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;else{<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;START_CRITICAL();&nbsp;&nbsp;&nbsp;/*&nbsp;make&nbsp;atomic,&nbsp;entering&nbsp;critical&nbsp;section&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*datapt=Fifo[GetI++];<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Size--;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(GetI&nbsp;==&nbsp;FifoSize)&nbsp;GetI&nbsp;=&nbsp;0;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END_CRITICAL();&nbsp;&nbsp;&nbsp;/*&nbsp;end&nbsp;critical&nbsp;section&nbsp;*/<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(-1);&nbsp;}<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 + -