C Language Study Notes (6)

Source: Internet
Author: User

1. pointer Source

In the contemporary era, most modern computers divide memory into bytes, each of which has its unique address. ExecutableProgramByCodeAnd data, and each variable in the program occupies one or more bytes, that is, each variable has its own address. This is the source of the pointer.

In fact, each address is a number, but we cannot use numbers to represent addresses. We need to use pointer variables to store addresses. For example, if int * P = I, we can say that p is a pointer variable and stores the address of variable I.

In int * p, p is the pointer variable pointing to an int type object.

2. Access operators and indirect addressing Operators

&, Address-taking operator. If X is a variable, & X is the address of variable X.

* Indirect addressing operator. If P is a pointer variable, * P represents the value of the variable pointed to by P.

When we declare int * P in the program, we didn't actually let P point to any address, but it's just a null pointer. We also call it a floating pointer. in the program, we should try to avoid such a situation, and try to initialize the statement at the same time. Otherwise, when * P is accidentally assigned in the future, if P points to a system memory address, the system will crash.

In fact, the first time we came into contact with the address fetch operator, it should be applied to scanf. Now let's think about the meaning of scanf ("% d", & A), which is actually to tell scanf function, the address to which the read value belongs.

3. Const protection parameters

We have mentioned that const represents a constant and cannot be changed. Therefore, we can apply const to pointer parameters. For example:

Void test (const int * P) indicates that * P cannot be changed, that is, the integer pointed to by P cannot be changed.

Void test (int * const p) indicates that P cannot be changed, that is, P can only point to this address.

Void test (const int * const p) indicates that P can only point to this address, and the data of this address cannot be changed.

4. pointer as return value

Here I will only mention one point. Do not regret the pointer variable of the local variable because the local variable is immediately recycled out of the scope, and then the pointer points to an obsolete address.

Of course, we can return the addresses of some parameters, external variables and static variables. In short, make sure that the pointer points to an unrecycled address.

 

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.