Storage of C-character arrays, character pointers, Fputs, fgets

Source: Internet
Author: User

Lesson NOTES: Strings stored in the C language (arrays and pointers) 1. Use a character array to store
1 Char name[5] = {' J ', ' A ', ' C ', ' K '}; 2 char name[] = {' J ', ' A ', ' C ', ' K '}; 3 Char // only when the character array is directly initialized with 1 strings will it be automatically added 4 char name[] = "Jack";
2. Use the character pointer to store the string array char *name = "Jack"; and char name[] = "Jack"; When both are local variables the array together with the array elements are applied in the stack area, but the content is an array that exists in the constant area, the address of the string where the pointer variable points to the constant area, and when both are global variables, the array also has a constant area (global variable). Array content is also in the constant area of the pointer in the constant area, the string in the form of an array also exists in the constant area, the pointer to the time the string corresponding to the address of a character array to store string data, whether global or local, can be subscript To modify the character array of each element in the form of a character pointer to store string data, this time the character pointer to the string data can not be modified, whether it is a global pointer variable or local pointer variable, is unable to modify the string data by the pointer to the constant premise of the string: is a string 1 stored as a character pointer. When the character pointer stores string data, both global and local, the string data is in the constant area; And once stored in the constant area, the string data can no longer change 2. When we store the string data in the constant area as a character pointer, we do not store the string directly in the constant area, but we first check if there is a string with the same content in the constant area if there is, just take the address of the string if not, then store the string data in the constant area 3. When we re-initialize the 1 string for the character pointer instead of modifying the original string, we re-create a string and assign the address of the new string to it 4.
1 char *name = "Jack"; 2
This is OK, but instead of changing "Jack" to "Rose", we re-create 1 "Rose" and assign the "Rose" address to name.
1 char name[] = "Jack"; 2 name = "Rose";
No, name is the array name, which is also the address of the array, and cannot be assigned directly to the group name but can be name[0] = ' R '; The number of times a character in a string appears in an interview question: 1. First, use a pointer variable to point to the string 2. Traversal stores a string into a file in 1.  Declare 1 file pointers, point to files on disk File *pfile = fopen ("File path", "Action on File") 2. Use the Fputs function to write the specified content to the file fputs (variable, pfile); 3. After writing, use the fclose () function to close the created file fclose (pfile); 4. Note: When the operation mode is "W", if the specified file does not exist, then create a file, if the file exists, directly overwrite the original contents of the file 5. Only if the operation mode is "a", if the file exists, it will append the new content, if it does not exist, then create the file

Storage of C-character arrays, character pointers, Fputs, fgets

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.