Basic string operations and basic string operations

Source: Internet
Author: User

Basic string operations and basic string operations

# Define _ CRT_SECURE_NO_WARNINGS # include <stdlib. h> # include <string. h> # include <stdio. h> // typical usage of a level-1 pointer // array int a [10] // string // 1 string of the C language that ends with zero // 2 no string type in the C Language simulate the memory allocation of the string // 3 string through the character array on the stack global zone (very important) // [] essence: it is the same as * p, but it conforms to the programmer's reading habits. // buf5 is a pointer, when the read-only constant buf5 is a constant pointer to analyze the memory, it ensures the safe release of the memory space pointed to by the buf. // Why? // P common pointer and memory first address difference // character array initialization void main51 () {// 1 specify the length of char buf2 [100] = {'A', 'B ', 'C', 'D'}; // 1-1char buf3 [2] = {'A', 'B', 'C', 'D '}; // if the number of initialization tasks is greater than the number of memory, the compilation fails. // 1-22 // The buf2 [4]-buf2 [99] 0 // 2 does not specify the length. C compiler will automatically help programmers find the number of elements char buf1 [] = {'A ', 'B', 'C', 'D'}; // buf1 is an array not a string ending with 0 printf ("buf2: % s \ n", buf2 ); printf ("buf2 [88]: % d \ n", buf2 [88]); printf ("hello .... \ n "); system (" pause "); return ;}// use a string to initialize the character array // strlen () length does not include 0 // sizeof () memory block size void main52 () {}// use the array subscript and pointer void main58 () {int I = 0; char * p = NULL; char buf5 [128] = "abcdefg"; // buffor (I = 0; I <strlen (buf5); I ++) {printf ("% c ", buf5 [I]);} p = buf5; // buf represents the address of the first element of the array for (I = 0; I <strlen (buf5); I ++) {p = p + I; printf ("% c", * p) ;}// buffor (I = 0; I <strlen (buf5); I ++) {printf ("% c", * (buf5 + I ));} // [] * derivation process // buf5 [I] ==> buf5 [0 + I] ;=> * (buf5 + I ); {// buf5 = buf5 + 1; // buf5 = 0x11;} printf ("hello .... \ n "); system (" pause ");}

 

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.