In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use c language string manipulation function". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Constant pointer and pointer constant
First of all, let's add that the previous article taught you an in-depth understanding of pointers in cAccord +. I'm talking about a problem in pointers. Some people say that constant pointers and pointer constants are always confused, for example:
Int a = 100 Const int * p = & a; / / constant pointer, the value pointed to cannot be changed, but the address pointed to can be changed int const * p = & a; / / equivalent to the above expression int * const p = & a; / / pointer constant, the address pointed to cannot be changed, but the value pointed to can be changed
So there is a trick to tell the difference between constant pointers and pointer constants, which I forgot to tell you in the last article:
From left to right, skip the type and see which character is modified. if it is *, the value of the pointer cannot be changed. if it is a pointer variable, the pointer cannot be changed and the value of the pointer cannot be modified. This principle can be popularly understood as the "nearest principle".
So looking back at the first line of code, that is, the pointer constant:
Const int * p = & a
We skip the variable type int, so const modifies *, so the value it points to cannot be modified.
The second line of code, constant pointer:
Int * const p = & a
Similarly, we skip int and find that const is directly modified by p, so its direction cannot be changed. There is a slight difference between the two, please pay attention.
Let's go back to the string issue in this section, and before we talk about the string copy function, let's recall the strings in the c language.
We know that there are two ways to define strings in the c language, namely:
Char str1 [] = "hello world"; / / Stack area string char* str2 = "hello world"; / / data constant area string
So what's the difference between the two in use? The answer is that after the first line is fixed, the operating system allocates stack memory to it, and if the second line defines a string in the form of a pointer, it allocates a memory area in the constant area of the data, meaning that its value is immutable:
Str1 [0] = 'mdistinct; / / correct, character array can be modified str2 [0] =' mdistinct; / / error, constant area cannot be modified
So, in the constant area, if we have two pointer variables with the same content but different variables, they actually point to the same block of memory:
Char* str1 = "hello world"; char* str2 = "hello world"; printf ("% p\ n", str1); printf ("% p\ n", str2)
In the above two lines of code, we print out the memory addresses pointed to by str1 and str2 respectively and find that their values are the same. Why? this is because the values in the constant area are read-only. Even if we declare two different variables, as long as their values are the same, then the two variables point to the same memory area.
It is worth noting that in C++, the string pointer is slightly different from that in the c language. In C++, the string pointer is enhanced directly, because it is stipulated in C++ that the string pointer must be modified with const. For example, if it is defined in C++, the compiler will report an error directly:
Char* str = "hello world"; / / Direct error const char* str = "hello world"; / / correct
In the actual development process, we generally use the array form of strings, but it is not recommended to use pointer strings, that is,
Char str [] = "hello world"; / / recommended char* str = "hello world"; / / not recommended
Therefore, please pay attention to the slight differences in this respect.
Second, the length of the string
We know that strings in C language end with'\ 0', which means that when you declare a string, the system will automatically add an ending character ending with'\ 0' to your end, and printf will check whether the current character is'\ 0' for each character printed until it stops as soon as it encounters'\ 0'. The most confusing thing here is the length of the string. Let's look at the following two lines of code:
Let's first look at the following two lines of code:
Char str1 [] = "hello"; char* str2 = "hello"; printf ("% d\ n", sizeof (str1)); / / output result is 6printf ("% d\ n", sizeof (str2)); / / output result is 4 or 8
So why are we using sizeof to calculate the string length, the two calculated results are not the same, and the first line length is not what we want, should not be 5? This is because when you declare a string, the system automatically adds an ending character ending with'\ 0' to your end. The memory model is as follows:
So, for the above two lines of code, they are actually 6 in length. So why is the output of the second line 4? this is because in the second line we define a string pointer that points to a string in a constant range, and when the sizeof operator operates on this pointer, it actually calculates the byte length of the pointer, and a pointer occupies a length of 4 bytes in x86 systems and 8 bytes in x64 environments, so In fact, when we calculate the length of a string, we usually use the strlen () function, but note that strlen also ends the string with'\ 0', that is, the strlen () function constantly determines whether the current character is'\ 0', and if so, it ends immediately. This feature is the same as the printf function, both of which end as soon as they encounter'\ 0':
Char str1 [] = "abc"; char str2 [] = {'a','\ 0','c'}; char str3 [] = {'a', 'baked,' caged,'\ 0'}; char* str4 = "abc"; printf ("% d\ n", strlen (str1)); / / output result is 3printf ("% d\ n", strlen (str2)); / / output result is 1printf ("% d\ n", strlen (str3)) / / output result is 3printf ("% d\ n", strlen (str4)); / / output result is 3
The above is the string length function in c language, which should be paid attention to in the process of use.
3. String copy function in c language 1) strcpy () # include char * strcpy (char * dest, const char * src); / / function: copy the string pointed by src to the space pointed to by dest, and'\ 0' will also copy the past parameter: dest: destination string first address src: source character first return value: success: return the first address of the dest string failed: NULL
The schematic code is as follows:
# define _ CRT_SECURE_NO_WARNINGS # include char str [10] = {0}; char str1 [] = "hello"; char* mystr = strcpy (str, str1); save the pointer returned by strcpy to mystr printf (mystr)
The memory model is as follows:
Because it is copied one by one, it means that the copy ends even if the'\ 0' character is encountered in the middle of the string.
There are two things to pay attention to: first, you must ensure that the memory space pointed to by dest is large enough, otherwise it may cause buffer overflow errors; second, because the strcpy function itself is an unsafe function, the compiler will pop up a warning. To ignore it, add macro definition code at the beginning of the program:
# define _ CRT_SECURE_NO_WARNINGS2), strncpy () # include char * strncpy (char * dest, const char * src, size_t n). Function: copy the first n characters of src pointing to the string into the space pointed to by dest. Whether to copy the Terminator depends on whether the specified length contains'\ 0'. Parameter: dest: destination string first address src: source character first address n: specify the number of strings to be copied return value: success: return the first address of the dest string failed: NULL
This function is similar to strcpy in that it is no longer cumbersome.
3), strcat () # include char * strcat (char * dest, const char * src); function: concatenate the src string to the end of the dest.'\ 0 'will also append the past parameter: dest: destination string first address src: source character first address return value: success: return the first address of the dest string failed: NULL
This is a string append function that appends the string pointed by src to the string pointed to by dest, as well as the Terminator'\ 0':
# define _ CRT_SECURE_NO_WARNINGS # include char str [] = "123"; char str1 [] =" hello "; char* mystr = strcat (str, str1); printf ("% s\ n% p ", mystr, mystr); / / output result: 123hello
However, it is also important to note that the target string dest must have a large enough buffer to receive, otherwise an error will be reported. The memory model is as follows:
4), strncat () # include char * strncat (char * dest, const char * src, size_t n); function: connect the first n characters of the src string to the end of the dest.'\ 0' will also append the past parameter: dest: destination string first address src: source character first address n: specify the number of strings to be appended return value: success: return the first address of the dest string failed: NULL
This function is similar to strcat, except that it specifies the amount to append.
5), strcmp () # include char * strcat (char * dest, const char * src); function: concatenate the src string to the end of the dest.'\ 0 'will also append the past parameter: dest: destination string first address src: source character first address return value: success: return the first address of the dest string failed: NULL
The function is to compare the ASCII codes of two strings and output different results, which are often used to determine whether two strings are equal. The example code is as follows:
Char * str1= "hello world"; char * str2 = "hello mike"; if (strcmp (str1, str2) = = 0) {printf ("str1==str2\ n");} else if (strcmp (str1, str2) > 0) {printf ("str1 > str2\ n");} else {printf ("str1"
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.