C language 13-pointers and strings

Source: Internet
Author: User

    • String review
    • One, all characters of the string are traversed with the pointer
    • Second, direct pointer to string
    • Third, the attention of the pointer processing string

Description: This C language topic is the prelude to learning iOS development. And for programmers with an object-oriented language development experience, you can quickly get started with C language. If you don't have programming experience, or are not interested in C or iOS development, please ignore.

String review

A string consists of one or more characters, so we can use a character array to hold the string, but at the end of the array we add a null character ' \ s '.

Char s[] = "MJ";

The above code defines a character array s to store the string "MJ", and the system automatically adds a null character to the trailing ' + '.

The memory distribution is roughly as shown in the illustration on the right:

From the previous article, "pointers to one-dimensional array elements," you can see that pointers and arrays are very closely related, so we can also manipulate strings using pointers.

One, all characters of the string are traversed with the pointer
1//define a pointer P 2 char *p; 3  4//define an array s to hold the string 5 char s[] = "MJ"; 6  7//pointer p to the first character of the string ' m ' 8 p = s;//or P = &s[0]; 9 for (; *p! = ') ; p++) {One     -by-one printf ("%c \ n", *p); 12}

After the 8th line is executed, the memory is distributed as shown on the right:

The basis of the preceding pointers and arrays is that you can see the code after the 9th line: Before each traversal, determine if the character P is currently pointing to is a null character, or if it is not a null character, print the current character, and then execute p++ so that the pointer p refers to the next character element.

The result of the final output:

Second, direct pointer to string

As you can see from the front, pointers can indeed point to strings and manipulate strings. However, the previous approach is to define a string array to hold the string, and then pass the first address of the array to pointer p, so that p points to the first character of the string.

1. We can also direct pointer to a string, omit the definition of the character array this step

1 #include <string.h> 2  3 int main () 4 {5     //define a string with pointer s pointing to this string 6     char *s = "MJ"; 7      8     //Using str The Len function measures the length of the string 9     int len = strlen (s);     printf ("String length:%d", Len);     return 0;13}

Note In line 6th, we direct the pointer s to the string "MJ", and do not create an array of characters first. Looking at line 9th, the pointer S is passed into the Strlen function, which indicates that the string processing function that was previously learned can still be used normally. Output Result:

2. Let's take a look at the declaration of the Strlen function in String.h

size_t     strlen (const char *);

The formal parameter in the Strlen function is a pointer type to a character variable, and in the character and string common handling function we can pass in a character array name, which illustrates the close relationship between pointers and arrays, and certainly JQ. In fact, when you call the Strlen function, you pass an address to it, and it starts counting the number of characters from this address until it encounters the null character ' \ s ', so passing in a pointer variable or array name is OK.

The other string handler functions are the same:

1 Char    *strcpy (char *, const char *);//String copy function 2 char    *strcat (char *, const char *);//string concatenation function 3 int     strcmp (c Onst char *, const char *); string comparison function

Their arguments are pointer types that point to character variables, so you can pass in pointer variables or array names.

Therefore, the printf function can still be used normally:

Char *s = "MJ";p rintf ("%s", s);

Output Result:

3. Other ways the pointer points to a string

Char *s;s = "MJ";

The point above is also correct: Define the pointer variable first, and then point to the string. If a character array is not allowed to do this, the following is an error:

1 char s[10];2 s = "MJ";

The compiler is sure to report the error of line 2nd, because S is a constant, representing the first address of the array, and cannot be assigned a value operation.

It is also important to note that the following practices are also incorrect:

1 char *s = "MJ"; 2 3 *s = "like";

The 3rd line of code made 2 errors:

    • The 3rd line of code is equivalent to the string "like" into the memory space of S point, as the 1th line of code can be seen, S points to "MJ" the first character ' M ', that is, a piece of char type S point of storage space, only 1 bytes, to "like" in 1 bytes of space, Positive memory Overflow
    • As you can see from the 1th line of code, the pointer s is pointing to the string constant "MJ"! So it is no longer possible to modify the string contents by pointers! Even *s = ' A ' seems to be the wrong way of writing, because s points to a constant string that does not allow the modification of characters inside it.

Third, the attention of the pointer processing string

Now want to change the string "LMJ" the first character ' l ' to ' l ', the solution is a variety of

1. The first option

1//define a String variable "LMJ" 2 char a[] = "LMJ"; 3 4//Change the first character of the string to ' l ' 5 *a = ' L '; 6 7 printf ("%s", a);

Normal operation of the program, output results:

2. Someone should be able to think of the second option immediately.

1 char *p2 = "LMJ"; 2 *p2 = ' L '; 3 4 printf ("%s", p2);

It seems to be possible, but this is the error code, which is wrong on line 2nd. First look at line 1th, pointer variable p2 point to a string constant, because it is a constant, so its internal characters are not allowed to modify.

Someone might have been blindfolded, here's the 1th line of code char *P2 = "LMJ"; the 2nd line of code in the first scenario char a[] = "LMJ"; not the same? It's not the same.

    • Char a[] = "LMJ"; define a String variable!
    • Char *p2 = "LMJ"; defines a string constant!

C language 13-pointers and strings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.