Talk C chestnuts together (70th back: C language instance -- string initialization function)
Hello, everyone. We talked about the string SEARCH example in the previous review. The example here is:String initialization Function. When you leave the rest of your time, your words will go right. Let's talk C chestnuts together!
When defining a local variable, We need to initialize the string. Of course, you can manually initialize the string, but the standard library provides us with a special initialization function: memset. Next we will introduce its usage.
Memset function usage
Memset function prototype: Void * memset (void * s, int c, size_t count)
Memset function usage: In the string pointed to by s, assign the first count character to c.
We can see that s is a void pointer. Therefore, this function can also assign values to a memory area. In addition, count is often represented by sizeof (type) * n, where type is the specific type of s and n is the number. This ensures that each memory area can be correctly assigned values.
It is not our style. Next, let's give a practical example:
# Define SIZE 16int main () {char str [SIZE]; // defines a local variable and does not initialize printf ("The old string is: % s \ n ", str); memset (str, 'A', SIZE * sizeof (char); printf ("The setted string is: % s \ n", str); return 0 ;}
The following is the result of the program running:
The old string is:-$]? T? // The setted string is: aaaaaaaaaaaaaaaa // The correct value is output after initialization.
From the above results, we can see that the output content before and after string Initialization is completely different.
DIY memset Functions
Void * diy_memset (void * s, char c, int n)
Next we will introduce the DIY style and define a memset function.
1. extract a character from the string indicated by s and assign a value to the character using c; 2. next, take the next character in s and continue the value assignment; 3. repeat Step 2 until the value is assigned n times.
The code I wrote is as follows:
void *diy_memset(void *s,char c, int n){ char *p = NULL; if(NULL == s) return NULL; p = s; while(n>0) { *p = c; p++; n--; } return s;}
Below is the code for the memset function in the standard library. Please compare it with our DIY code:
/** * memset - Fill a region of memory with the given value * @s: Pointer to the start of the area. * @c: The byte to fill the area with * @count: The size of the area. * * Do not use memset() to access IO space, use memset_io() instead. */void *memset(void *s, int c, size_t count){ char *xs = s; while (count--) *xs++ = c; return s;}
Through comparison, you can find that the code of the standard library is more concise, but the assignment ideas of the two are the same.
The readers will not write the code in the body. The detailed code is put in my resources. You can click here to download it. The following is the running result of the program. For details, refer:
The old string is :-? _?? W? // The setted string is: aaaaaaaaaaaaaaaa // The setted string is: bbbbbbbbbbbbbbbb // The result of The initialization of The standard library function
For more information, see the string initialization function example. I want to know what examples will be provided later, and I will try again.