C language string pointers and sample code _c language

Source: Internet
Author: User
Tags constant strlen

C language does not have a specific string type, we usually put the string in a character array, which is in the "C language character array and string" has been explained in detail, here may be another demonstration:

#include <stdio.h>
int main () {
 char str[] = "http://c.biancheng.net";
 int len = strlen (str), I;
 Direct output String
 printf ("%s\n", str);
 Output one character for
 (i=0; i<len; i++) {
  printf ("%c", str[i) each time;
 }
 printf ("\ n");
 return 0;
}

Run Result:

Http://c.biancheng.net
Http://c.biancheng.net

A character array is ultimately an array, and the rules on pointers and arrays in the previous section also apply to character arrays. Change the code above, using pointers to output strings:

#include <stdio.h>
int main () {
 char str[] = "http://c.biancheng.net";
 char *pstr = str;
 int len = strlen (str), I;
 Use * (Pstr+i)
 for (i=0 i<len; i++) {
  printf ("%c", * (Pstr+i));
 printf ("\ n");
 Use Pstr[i] for
 (i=0 i<len; i++) {
  printf ("%c", Pstr[i]);
 printf ("\ n");
 Use * (Str+i)
 for (i=0 i<len; i++) {
  printf ("%c", * (Str+i));
 printf ("\ n");
 return 0;
}

Run Result:

Http://c.biancheng.net
Http://c.biancheng.net
Http://c.biancheng.net

In addition to character arrays, the C language also supports another way of representing strings, which is to directly use a pointer to a string, for example:

Char *str = "http://c.biancheng.net";

Or:

Char *str;
str = "Http://c.biancheng.net";

All characters in a string are contiguous in memory, and STR points to the No. 0 character of the string; We usually refer to the address of the No. 0 character as the first address of the string. The type of each character in the string is char, so the type of STR must also be char *.

The following example shows how to output such a string:

#include <stdio.h>
int main () {
 char *str = "http://c.biancheng.net";
 int len = strlen (str), I;
 
 Direct output String
 printf ("%s\n", str);
 Use * (Str+i)
 for (i=0 i<len; i++) {
  printf ("%c", * (Str+i));
 printf ("\ n");
 Use Str[i] for
 (i=0 i<len; i++) {
  printf ("%c", Str[i]);
 printf ("\ n");
 return 0;
}

Run Result:

Http://c.biancheng.net
Http://c.biancheng.net
Http://c.biancheng.net

All this looks like a character array, and they all can output the entire string using%s, and you can use * or [] to get a single character, is there no difference between the two ways of representing strings?

Yes! Their most fundamental difference is that the storage area in memory is different, the character array is stored in the global data area or the stack area, and the second form of string is stored in the constant area. The string (and other data) of the global data area and stack area has read and write permissions, while the string (and other data) of the constant area has only read permission and no write permission.

About the global data area, stack area, constant area and other memory partitions, we will be in the "C Language and memory" topics in detail, I believe you will have an epiphany, fundamentally understand the C language.

One obvious result of the difference in memory permissions is that a character array can read and modify each character after it is defined, whereas for a second form of string, once defined, it can only be read and cannot be modified, and any assignment to it is wrong.

We refer to the second form of string as a string constant, which means that constants can only be read and cannot be written. Take a look at the demo below:

#include <stdio.h>
int main () {
 char *str = "Hello world!";
 str = "I love c!"; Correct
 str[3] = ' P ';//Error return
 0;
}

This code compiles and links correctly, but there is a segment error (Segment Fault) or write location error at run time.

The 4th line of code is correct, you can change the pointer variable itself to point to, the 3rd line of code is wrong, you cannot modify the characters in the string.

Whether to use a character array or a string constant

If only the reading of strings is involved in the programming process, both character arrays and string constants can satisfy the requirements, and if there is a write (modify) operation, only character arrays can be used, and string constants cannot be used.

The string that gets the user input is a typical write operation that can only use a character array and cannot use string constants, see the following code:

#include <stdio.h>
int main () {
 char str[30];
 Gets (str);
 printf ("%s\n", str);
 return 0;
}

Run Result:

C C + + Java Python JavaScript
C C + + Java Python JavaScript

Finally, we conclude that the C language has two ways of representing strings, one is character array, the other is string constants, they are stored in memory in different places, so that character arrays can be read and modified, and string constants can only read cannot be modified.

The above is the C language string pointer data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

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.