There are two methods in C that represent strings, one is a character array, and the other is a string constant
The character array and string constants are sufficient to meet the requirements in the programming process if only the strings are read, and if there is a write (modify) operation, only character arrays are used, and string constants cannot be used.
The difference between a string array and a string constant:
The fundamental difference is that in memory the storage area is different, the character array is stored in the global data area or the stack area, and the second form of the string is stored in the constant area. The string (and other data) of the global data area and the stack area has read and write permissions, while the constant-area string (which also includes other data) has only read permission and no write permission.
The following example shows how to output such a string:
- #include<stdio.h>
- intmain() {
- Char*str ="Http://c.biancheng.net";
- int len =strlen(str), i;
- ?
- Direct output string
- printf("%s\n", str);
- Using * (Str+i) is a string array
- for (i= 0; I< len; I ++) { /span>
- printf("%c",* (str+i));
- }
- printf("\ n") ;
- Use Str[i] is a string constant
- for (i=0; i<len; i+ +) {
- printf("%c", str[i]);
- }
- printf("\ n") ;
- ?
- return 0;
- }
Operation Result:
Http://c.biancheng.net
Http://c.biancheng.net
Http://c.biancheng.net
The string that gets the user input is a typical write operation that uses only character arrays and cannot use string constants, see the following code:
- #include<stdio.h>
- intmain() {
- Char str[n];
- Gets(str);
- printf("%s\n", str);
- ?
- return 0;
- }
Operation Result:
C C + + Java Python JavaScript
C C + + Java Python JavaScript
Whether to use a character array or a string constant