C language Pointer tutorial----Getting started to master < one >

Source: Internet
Author: User

What is a pointer?

The pointer is also a variable, but this variable stores an address that points to a storage unit. We can find the variable that he refers to by the pointer (address) and manipulate the variable (assignment, etc.). it is convenient to represent the relationship between the pointer and the variable that the pointer refers to by taking the address symbol in C & and taking the symbol of the indicated content * .


Second, why use pointers?

Why do you use pointers? What benefits do pointers bring? The following content refers to the contents of the textbook:

" The correct and flexible application of the pointer, can effectively represent the complex data structure, the ability to dynamically allocate memory, convenient use of strings, efficient and convenient use of the array, when the function is called to obtain more than one result, can directly handle the memory unit address, etc.

the above summary should be considered more complete, but for beginners of C may be understood to be very abstract (may not understand what is the data structure); We try to get from the simplest-----" get more than one result when calling a function "To see how the pointer works.


1 Value passing: In C, the method of "value passing" is always taken when a function is called. This means that the actual argument is not directly involved in the function (which participates in the various operations within the function), except that the argument passes his value to the parameter to participate in the function and completes the operation within the function ( the parameter is allocated memory when the function is called, and the value of the formal parameter is the copy of the argument); Formal parameters and arguments are not a variable, do not occupy the same memory, and the parameters do not necessarily change. It's like three variables A,b,c performed the steps: After B=a;b=c, a does not necessarily equal C)

Example 1: Take a look at the following code. The code is to achieve two number (A, b) of the exchange, so the Exchange function swap is constructed, but can accomplish the purpose?


Example 2: What then? The application pointer. Understand the following program, he can do a good job of digital exchange.


There may be a reunion question, " why is this possible?" Here the function call is also the value passing AH ".

Right. Here too but the value is passed, but in the swap, it is manipulated by the pointer to the variable pointed to, (you can give him a name-----the hand of God) in your mind to understand that we are in the function call, not X and y each other

Exchange, instead of the two pointers, the content they are referring to (that is, A, b itself) is swapped.

Example 3: If you understand what I said above, and then look at the following code, can he complete the task?


What do you think? is not a bit confused. Let me help you with your ideas. Even if you use pointers for function arguments, function calls are still value-passed. Example 2, x, y point to A, B, through *x,*y can change a, B, example 3,x, y points to a,b,swap exchange X, Y, but this to the main function c,d pointer and variable A, no change.

C language Pointer tutorial----Getting started to master < one >

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.