Use a pointer to reference a string.

Source: Internet
Author: User

Use a pointer to reference a string.
(1) a small example of a pointer referencing a string

Copy string a to string B

1 # include <stdio. h> 2 int main () {3 char a [] = "I am a boy", B [20], * p1, * p2; 4 p1 =; p2 = B; // p1, p2 points to the first element 5 for (; * p1! = '\ 0'; p1 ++, p2 ++) 6 * p2 = * p1; 7 * p2 =' \ 0'; 8 printf ("% s ", b); 9}
(2) character pointers as function parameters

You can select the character array name and character pointer variable for both the real parameter and the form parameter, but there is a difference:
(1) Several storage units are allocated to the character array during compilation to store the values of each element. For character pointer variables, only one storage unit is allocated.
(2) The value of the pointer variable can be changed, while the array name represents a fixed value (the address of the first element of the array) and cannot be changed.

Char * a = "I am a student" a = a + 7; // valid char str [] = {"I am a student "}; str = str + 7 // invalid

(3) The values of each element in the character array can be changed, but the content of the String constant pointed to by the character pointer variable cannot be changed.

Char a [] = "house"; char * B = "house"; a [2] = 'R'; // valid * (B + 2) = 'R '; // invalid

Next, we will introduce an example using the character array name as a function parameter to replicate strings.

1 # include <stdio. h> 2 int main () {3 void copy_string (char from [], char to []); 4 char a [] = "I am a teacher "; 5 char B [] = "you are a student"; 6 7 copy_string (a, B); // copy a to B 8 printf ("% s \ n % s ", a, B); 9} 10 void copy_string (char from [], char to []) {11 int I = 0; 12 while (from [I]! = '\ 0') {13 to [I] = from [I]; I ++; 14} 15 to [I] =' \ 0'; 16}
(3) variable format Output Functions
char *format;format="a=%d,b=%d\n";printf(format,a,b); 

Variable format output function, you only need to change the string pointed to by the format, you can change the input and output format

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.