Pointers to small secrets in C Language (3)

Source: Internet
Author: User

But mortals are eager for success and inertia. I am an example. I don't want to write a summary and text descriptions for each blog, but I still want to let some new readers know the content of my blog as I mentioned earlier, I moved the first summary about pointers, because I wrote these articles about pointers, so there is no need for each abstract, so I am secretly lazy here, if you have read the previous two C-pointer blogs, you can skip the abstract before this blog and directly go to the topic section.

Anyone who knows C language knows that C language is powerful and free, most of which are reflected in its flexible pointer usage. Therefore, pointer is the soul of the C language, and it cannot be said at all. So I added a title (1) to show the importance of pointers. I try my best to explain my understanding of pointers. So in the course of explanation, I try my best to use the code and text description, and analyze the code to deepen our understanding of the pointer. All the code I provide is complete, therefore, you can directly copy the file to run it during the reading process. I hope the following explanation will help you.

In C, we can use two methods to access a string.

1. Store a string with a character array

Char STR [] = "this is str !!! ";

Here STR is an array name, representing the first address of the string array.

2. Use a character pointer to point to a string

Char * STR = "this is str, too ";

The C language processes string constants in the form of character arrays. A character array is opened in the memory to store string constants. Here STR is defined as a pointer variable pointing to the character array, which can only point to one character variable and other character data. Printf ("% s", STR); is used for output. During this process, the system first outputs the character data pointed to by a STR, and then outputs the next character using the STR plus 1 method, when the '\ 0' string Terminator is encountered, an' \ 0' is automatically added at the end of the string in the memory '.

You can use the address transfer method to transmit strings. Use the name of the character array or the pointer variable pointing to the character array as the parameter. The content of the string can be changed in the called function. The main function can obtain the changed string.

I. Using character arrays as parameters

Next let's take a look at the Code:

# Include <stdio. h>

Void copy_string (char from [], Char to [])
{
Int I = 0;
While (from [I]! = '\ 0 '){
(To [I] = from [I]);
I ++;
}
 
To [I] = '\ 0 ';
 
Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

The running result is:

There is no difficulty in code implementation, but there may be many people who may forget the code part marked with red in our code, thus making an error.

Another implementation method is provided here:

# Include <stdio. h>

Void copy_string (char from [], Char to [])
{
Int I = 0;
While (to [I] = from [I ++])! = '\ 0 '){
;
}
 
Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

The running result is:

It is worth noting that this implementation method cleverly utilizes the while statement while (to [I] = from [I ++])! = '\ 0 '). Do not write it as while (to [I ++] = from [I ++])! = '\ 0'). In this way, I ++ is executed twice each time, leading to an error in the final result.

2. Use character pointer Variables

 

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Int I = 0;
While (* from! = '\ 0 ')
* To ++ = * From ++;
* To = '\ 0 ';

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

The running result is:

Here I also provide a reference code that combines a comma expression and a for loop statement:

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Int I = 0;
For (; * To = * From, * from! = '\ 0'; from ++, to ++ );

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

 

The running result is:

The code is cleverly implemented by combining a comma expression and a for loop statement, because the result of a comma expression is the result of the last expression, therefore, when executing the comma expression in the judgment statement, the value is still * from! = '\ 0 '.

If you are interested, you can try other implementation methods. The following describes several clever implementation methods. If you are interested, you can study their implementation principles by yourself, all of which are complete code.

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Int I = 0;
For (; * To ++ = * From ++ ;);

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

 

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Int I = 0;
While (* To ++ = * From ++ );

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

 

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Int I = 0;
While (* To ++ = * From ++ )! = '\ 0 ');

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

 

# Include <stdio. h>

Void copy_string (char * From, char *)
{
Char * P1, * P2;
P1 = from;
P2 =;
While (* P2 ++ = * P1 ++ )! = '\ 0 ');

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

 

# Include <stdio. h>

Void copy_string (char from [], Char to [])
{
Char * P1, * P2;
P1 = from;
P2 =;
While (* P2 ++ = * P1 ++ )! = '\ 0 ');

Return;
}

Int main ()
{
Char STR [] = "this is a string! ";
Printf ("% s \ n", STR );
Char dec_str [206];
Copy_string (STR, dec_str );
Printf ("% s \ n", dec_str );
Return 0;
}

I will not post the running results here. The running results are the same as above. If you are interested, you can study the above Code and there are many implementation methods, we hope that the above Code can inspire readers to write more implementation methods and better implementation solutions.

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.