I also want to learn C language-Chapter 2: pointers, strings, and chained expressions

Source: Internet
Author: User

This morning, I learned the pointer and string parts, which are used to enhance the understanding of the pointer and string through some examples.

Let's first look at an example:

# Include <stdio. h>

Void main (){

Int a [] = {6, 60,600 };

Int * p =;

* P ++; printf ("% d", * p );

}
What are the results of this program ?! There are three elements in the array. P points to the first address of the array and then * p ++. What will be output ?! Hey! Of course, output 60, because ++ is combined first, ++ is used to get the pointer of the second element, and then the content is output 60.

If we want to add the first element 6 to 1, we should write:

# Include <stdio. h> void main () {int a [] = {6, 60,600}; int * p = a; (* p) ++; printf ("% d ", * p );}
This example is mainly about consolidating the priority and the relationship between pointers and arrays.

Copy string

The following is an example of copying strings:

# Include <stdio. h>

Void str_cpy (char * pDest, char * pSrc ){

While (* pDest ++ = * pSrc ++);} void main (){

Char sz1 [32];

Char sz2 [] = "Hello world ";

Str_cpy (sz1, sz2 );

Printf ("% s", sz1 );

}
Pay attention to the analysis. * pSrc ++ here is the suffix. It is the last plus 1. When will it exit the while loop ?! Of course, when the left value is 0. Do you think the function for copying strings is very concise? Just one line! Hey!

Advantages of copying strings from the original system

If we query the copy string of the system's original version, it has one more thing than ours, that is, it returns one char *. Let's see where the original version is better:

# Include <stdio. h>

# Include <string. h>

Void str_cpy (char * pDest, char * pSrc)

{

While (* pDest ++ = * pSrc ++);} void main ()

{

Char sz0 [32];

Char sz1 [32];

Char sz2 [] = "Hello world ";

Strcpy (sz0, strcpy (sz1, sz2 ));

Printf ("% s", sz0 );

}
Copying is actually a value assignment operation like an integer. The value of the value assignment operation is equal to the left value. This function of the system returns a left value. In this way, the system can copy strings to implement chained expressions.

Some friends said that they couldn't keep up. I think the reason is that you didn't debug it !, You can debug and analyze the memory if you have any questions! After two weeks of study, I have the following learning experience. When you encounter a problem, you should first debug and analyze the memory to solve the problem. Do not go to the so-called Masters' explanation !, Because most masters are trying to show off technology! Their goal is to make you more ignorant. Debugging and analyzing the memory by yourself is the fastest way to learn, because what the memory looks like, then what it looks like! It is important to trust yourself and machines. Prove the theory of masters with practice. Without debugging and memory analysis, you will lose yourself if you blindly follow the words of the master. There is a saying that to become a master, you need to imitate the master, but the imitation is the hard work of the master before he has become a master, A lot of things will suddenly become open when debugging 1! I did not understand many concepts once, because I did not become my own. I used debugging to learn, you will see where the data actually exists in the memory, and you will be very steadfast!

 

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.