Section1: String Fundamentals
1.1 String Declaration
1.2 The String Literal Pool
1.3 String Initialization
Section2: Standard String Operations
2.1 Comparing Strings
2.2 Copying Strings
2.3 Concatenating Strings
Section3: Passing Strings
3.1 Passing a Simple String
3.2 Passing a Pointer to a Constant char
3.3 Passing a String to be Initialized
3.4 Passing Arguments to an Application
Section4: Returning Strings
4.1 Returning the Address of a Literal
4.2 Returning the Address of Dynamically Allocated Memory
Section5: Function Pointers and Strings
Section1: String Fundamentals
Arrays of char have been used to represent smaller integer units, such as boolean, to conserve memory space in an application.
Two types of string in C:
(1) Byte string: Consists of a sequence of char data type; in string.h
(2) Wide string: Consists of a sequence of wchar_t data type. in wchar.h
wchar_t is used for wide characters and may be either 16 or 32 bits in width.
Both of these strings are terminated by the NUL character.
The length of string does not include the NUL character.
PS:
NULL : ((void*)0)
NUL: ‘\0'
Character constants are character sequences enclosed in single quotes. In C, they are of type int.
1.1 String Declaration
three ways:
(1) as a literal. “adbde"
(2) as an array of characters. char header[32];
(3) using a pointer to a character. char *header;
The string literal is a sequence of characters enclosed in double quotes.
1.2 The String Literal Pool
GCC uses a -fwritable-strings option to turn off string pooling.
In Microsoft Visual Studio, the /GF option will turn on string pooling.
In most compilers, a string literal is treated as a constant. However, in some compiler, such as GCC:
char *tabHeader = “Sound”;
*tabHeader = ‘L’;
printf(“%s\n”, tabHeader); // Displays “Lound"
一般string当作常量,但如上所示, GCC可改。 一种做法是:
const char* tabHeader = “Sound”;
1.3 String Initialization
1.3.1 Initializing an array of char
(1) char header[] = “Media Player”;
(2) using strcpy
char header[13];
strcpy(header, “Media Player”);
1.3.2 Initializing a pointer to a char
(1) normal
char *header = (char*) malloc(strlen(“Media Player”) + 1);
strcpy(header, “Media Player”);
(2) directly
char *header = “Media Player”;
1.3.3 Initializing a String from standard input
char *command = (char*) malloc(100);
printf(“Enter a Command: “);
scanf(“%s”, command);
1.3.4 summary of string placement
Section2: Standard String Operations
2.1 Comparing Strings
prototype: int strcmp(const char *s1, const char *s2);
returns:
Negative : if s1 precedes s2 lexicographically (alphabetically)
Zero: if the two strings are equal
Positive : if s1 follows s2 lexicographically
There are a couple of incorrect ways to compare two strings.
(1) if (command = “Quit”) // error, it is an assignment, set the address of string “Quit” to the array ( array name) - command;
(2) if (command == “Quit”) // return false; 300 <> 600
2.2 Copying Strings
prototype:
char* strcpy(char *s1, const char *s2)
2.3 Concatenating Strings
prototype:
char* strcat(char* s1, const char* s2)
Section3: Passing Strings
3.1 Passing a Simple String
size_t stringLength(char string[]) // 这是一样的
3.2 Passing a Pointer to a Constant char
size_t stringLength(const char* string);
3.3 Passing a String to be Initialized
An alternative to this approach is to pass NULL;
3.4 Passing Arguments to an Application
显然,也可以写成:
int main(int argc, char* argv[ ]) {
Section4: Returning Strings
return a reference to either:
(1) A literal
(2) Dynamically allocated memory
(3) A local string variable
4.1 Returning the Address of a Literal
4.2 Returning the Address of Dynamically Allocated Memory
4.3 Returning the Address of Local String
chapter3 section2 2.5 说明了a pointer to local variable, and then return it will cause error.
Section5: Function Pointers and Strings
it’s in the ctype.h
Returns an integral value indicating the relationship between the strings:
return value |
indicates |
<0 |
the first character that does not match has a lower value in ptr1 than in ptr2 |
0 |
the contents of both strings are equal |
>0 |
the first character that does not match has a greater value in ptr1 than in ptr2 |
|
|
负数表更小的值, acsii码越小,字母越靠前哪
ascii码表表示:从小到大顺序为 符号-> 数字->大写字母->小写字母。符号不一定,较乱。