Use string in C string in C is very much related to the cursor. When you're used to using the cursor, you can transport chain used to handle more efficiently. A string in C is simply an array of characters. The following line declares an array that can hold a string music boxes of up to 99 characters.
Str array of characters stored as follows: str [0] is the first character of the array, str [1] is the second character, and so on. But why a 100-element array to hold up to 99 characters. Because C uses null-terminated strings, that is the end of a string is marked by characters with ASCII code 0 (the null character), which is represented in C '' music boxes
In C, as already said, we can not simply assign one array to another array, where each element to copy. Library strcpy string containing allows you to do this. The following code shows how to use strcpy function to copy the string: #include <string.h> void main () {char s1 [100], s2 [100]; strcpy (s1, "hello"); / * Copy "hello" into s1 * / strcpy (s2, s1); / * Copy s1 into s2 * /}
Strcpy function is used when you need to initialize a chuoi.Trong C, you must use the strcmp function in the string library, which compares two strings and returns an integer that indicates the result of the comparison. The result music boxes is 0 means that two strings are equal, a negative value means that s1 s2. #include <string.h> void main () {char s1 [100], s2 [100]; gets (s1); gets (s2); if (strcmp (s1, s2) == 0) printf ("equal \ n"); else if (strcmp (s1, s2) <0) printf ("s1 is less than s2 \ n"); else printf ("s1 is greater than s2 \ n"); }
Some other common functions in the string library strlen, which returns the length of the string; strcat chain connecting the two. You can refer to the help section of Turbo C.Ban need to build some string handling functions for himself. Let's start with rewriting strlen. Here is a coding style you already familiar with Pascal: int strlen (char s []) {int x; x = 0; while (s [x]! = '') x = x + 1; return (x); }
Most C programmers do not like this approach because it seems inefficient. Instead, music boxes they often use an approach based on a pointer: int strlen (char * s) {int x = 0; while (* s! = '') {x ++; s ++; } Return (x); }
However, the actual running and compare three programs music boxes above, you will see their time performing the same or very small difference. That means, music boxes you should write code in any way you see most understandable code. Pointers generally make programs run faster, but strlen does not belong in that case.
Note the <= because this code to copy the characters ''. Another thing that this code is very inefficient, because strlen gets called every time you repeat loop. To solve this problem, you can use the following code: strcpy (char s1 [], char s2 []) {int x, len; len = strlen (s2); for (x = 0; x <= len; x ++) s1 [x] = s2 [x]; }
Even you can say while (* s1 ++ = * s2 ++); This time, when the trial, you will see the first version to run very slowly, while the third edition and fourth faster than the second version. In this case, the cursor really effective.
Use the cursor to create chains sometimes markedly effective rate. For example, suppose you want to remove leading blanks in a string. In C, you often have to use the delete function as follows: #include <stdio.h> #include <string.h> void main () {char s [100], * p; gets (s); p = s; while (* p == '') p ++; printf ("% s \ n", p); }
Two code even give the same results, but their behavior is completely different. In paragraph 2, you can not write s = "hello" ;. To understand the difference, music boxes you need to know the operation of the constant table (string constant table) in C.
When the program is executed, the compiler creates an object file containing machine code and a string table containing all constants music boxes declared in the program. music boxes In paragraph 1, the statement s = "hello"; determined that s only to the address of the string hello in the constant table. Because this sequence is located in the constant table, and is part of the executable code, so you can not change it. You can only use it according to the type of read-only (read-only).
This code compiles, but it generates an error "segmentation fault" right in line free. Because malloc to create a block of 100 bytes of memory and point s on it, but the line s = "hello"; s points to a string in table string constants, and memory blocks 100 bytes skipped. Thus free fails because it can not liberate a memory block in an executable region.
Answer
Subscribe
% D bloggers like this:
No comments:
Post a Comment