Brief introduction of string using C

0.子串vs公共子序列

1.字符串基礎

  • 字符串常量: 以雙引號擴起來的字符序列,規定所有的字符串常量都由編譯器自動在末尾添加一個空字符
  • 字符數組: 末尾添加了'\0'的字符數組,一般需要顯示在末尾添加空字符。
  • 規定C風格的字符串都是以NULL空字符('\0')作爲終結符結尾。
  • 所以字符串的長度並不包括NULL字節,如strlen函數

2 標準庫中的字符串處理函數

C標準庫中頭文件<string.h>定義了兩組字符串函數(C++中用<string>表示)。

  • 第一組函數的名字以str開頭,它主要處理以'\0'結尾的字符串,所以字符串內部不能包含任何'\0'字符。
  • 第二組函數的名字以mem開頭,主要考慮非字符串內部含有零值的情形,它能夠處理任意的字節序列,操作與字符串函數類似
  • 除了memmove函數外,其他函數都沒定義重疊對象間的行爲
  • 爲了提高程序在不同機器上的移植性,利用typedef定義新類型名,即typedef unsigned int size_t
  • size_t:
  • void *malloc(size_t n);
    void *memcpy(void *s1, void const *s2, size_t n);
    size_t strlen(char const *s);
  • General Information(B.2.12)

    Defined Types
    The requirement that additional types defined in this section end in "_t" was prompted by the problem of name space pollution. It is difficult to define a type (where that type is not one defined by POSIX.1-2008) in one header file and use it in another without adding symbols to the name space of the program. To allow implementors to provide their own types, all conforming applications are required to avoid symbols ending in "_t", which permits the implementor to provide additional types. Because a major use of types is in the definition of structure members, which can (and in many cases must) be added to the structures defined in POSIX.1-2008, the need for additional types is compelling.
    The types, such as ushort and ulong, which are in common usage, are not defined in POSIX.1-2008 (although ushort_t would be permitted as an extension). They can be added to &amp;amp;lt;sys/types.h&amp;amp;gt; using a feature test macro (see POSIX.1 Symbols). A suggested symbol for these is _SYSIII. Similarly, the types like u_short would probably be best controlled by _BSD.

 

Explain:while(*pdst++)爲假時,*src='\0'賦值給了pdst.

Notes:strncpy默認n<=strlen(dst)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章