C language pointer guide (1) -- what is a pointer?

Source: Internet
Author: User

Please repost the source from a friend

I. What is a pointer?

It is important to clarify what a pointer is. This is the source of the topic we are discussing. Before explaining what a pointer is, let's take a look at the concept of a variable.

We know that the computer's memory (primary storage) is divided into multiple storage units, which can be used in a single or sequential connection to form a larger unit. Each individual storage unit is a byte, which usually consists of eight bits. Each bit can represent only 0 or 1. Each storage unit is uniquely represented by an identifier assigned to it, which is an address.

It indicates the case when the storage unit is operated independently. The rectangle indicates the storage unit, and the content in the rectangle is the specific value stored in the memory unit. The number above the rectangle is the address of each memory unit. Because each unit is a byte, and each character constant occupies exactly one byte, as shown below:


Let's take a look at the figure below:

In this case, the operation is performed sequentially in A group. For integer constant, four bytes are required for storage on a 32-bit computer (one thing to declare is that, the 208 in the rectangle at the position of 1078345 is out of the int type range and is of the long int type. However, ansi c only specifies that the long data length is not smaller than the int type, int type data is not less than short type, and the int type is set to 16 bits, long type is 32 bits, however, many compilers adopt the policy to make long and INT data occupy the same number of memory bytes, that is, all 32 bits ), therefore, the address increases in 4 units (that is to say, the current rectangle represents 4 memory units). This time, there are several lower-case letters under the rectangle, and the values stored in the rectangle are not fixed and unique, it is variable. We can think of the rectangle as a variable. Every time we reference the value in the rectangle, the machine uses the address to locate it and obtain the value, for us, it is almost impossible to remember these addresses. Therefore, advanced Languages provide the feature of using names to access memory locations. They are variable names, namely, a, B, c, d.

Replace the following address with the variable name:

Note that the association between variable names and addresses is implemented by the compiler. We do not need to care about the specific implementation method, but we need to know that the hardware still accesses the memory location through the address. Next, continue to see the figure:

The newly added PTR is also a variable with an address. Its value is the address of variable. Now we can define the pointer:A pointer is a variable used to store the address of another variable.PTR in is a pointer, and we say it points to variable A (because the PTR value is the address of variable A). Note that the pointer can only store the address, you cannot assign a pointer to data of an integer or other non-address type (except integer 0 and an integer constant expression with a value of 0, which will be detailed in the following article!

Also, the pointer word is translated for the pointer word. The complete name should be the pointer variable. Because of the address saved in the pointer variable, in most foreign documents, the meanings of pointers are not all pointer variables, but also addresses. Please be careful when reading or referring to documents!

(Note: In this section and subsequent chapters, the values in the rectangle used to represent the memory are actually expressed in binary numbers, and the addresses are expressed in hexadecimal numbers, in this article, we only use the decimal number for the sake of simplicity and ease of reading .)

Preface

2. pointer definition and Operation

3. pointer and array "Love and hate"

4. distinguish between function pointers and pointer Functions

5. pointer and Structure

6. "trap" when using pointers"

Postscript

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.