[Know why] semantics & quot; Traps & quot; --- arrays and pointers

Source: Internet
Author: User

Arrays and pointers often appear in programming languages. Maybe when I was in class, the teacher also said that arrays and pointers are different and should also be discussed in reference books. Have you ever thoroughly clarified them?

This blog post mainly points out the differences in access methods between arrays and pointers from the perspective of memory and compilation.The differences between function calls and their connections will be discussed in detail in the next article.

Okay. This blog post will introduce my understanding of arrays and pointers. Hope to help you.

To make it clearer, I will talk about some basic parts.

What's a Declaration? What's a Definition?Description and definition

Objects in the C language must have only one definition, but there can be multiple declarations (extern). The objects mentioned here are irrelevant to objects in the object-oriented language.

A definition is the special kind of declaration that creates an object; a declaration indicates a name
That allows you to refer to an object created here or elsewhere.

Definition is a special declaration. It creates an object. The declaration simply describes the name of the object created elsewhere. It allows you to use this name.

It can be understood as follows:

StatementDeclaration: Describes the objects created elsewhere and does not allocate memory. (It can appear in multiple places)

DefinitionDefinition: Generate a new object and allocate memory. (Only once)

 

How Arrays and Pointers Are Accessed-How to access Arrays and Pointers

The access methods of arrays and pointers in the memory are different. Note the difference between "address y" and "address y content. "Address y" indicates the address of variable y in memory, while "address y content" indicates

The content in the address, that is, the value of variable y. Most programming languages use the same symbol to represent these two things, and the compiler determines its meaning based on the context.

Take a simple assignment as an example:

In the preceding section, x indicates the address represented by x, and y indicates the content of y.

The value displayed on the left of the value assignment symbol is called the left value, and the value on the right is called the right value. The compiler assigns an address (left value) to each variable ). This address is known and always exists during compilation, and its right value is displayed at runtime.

In order to know. In layman's terms, each variable has an address, which can be known during compilation, and the content stored in the address (that is, the value of the variable) can be known only at runtime. If you need to use variables

(That is, the value stored in the known address), the compiler sends a command to read the variable value from the specified address and put it into the corresponding register.

The key here is that the address is known during compilation. If you want to perform some operations (such as adding an offset), you can directly perform operations. On the contrary, for pointers, you must obtain its address at runtime before

Reference it. Displays references to the array objects:

In this way, we can explain why extern char a [] is the same as extern char a [100. Both of these are table name a and an array, that is, a memory address,

The characters in the array can be found at this address.

Different from the above, if we declare a pointer, such as extern char * p, it indicates that p points to a character. To obtain this character, we must know the content of address p, use it as the character address

And get this character from this address.

If it is an array a [], you can directly use an array name to access the elements in the array, because its content is the first element, and its next address corresponds to the address of the next array element.

If the pointer is * a, the content of address a needs to be taken out first, and then taken as the address of the variable and the content of the variable is obtained from the address.

 

Other differences between arrays and pointers:

 

When defining a pointer, the compiler does not allocate space for the object it points to, but only allocates space for the pointer itself. Unless you pay a pointer to a one-character escape constant for initialization at the same time as the definition.

For example, char * p = "breadfruit ";

Generally, the string variable created during pointer Initialization is defined as read-only. If you try to modify it, there will be undefined behavior.

 

This article mainly summarizes the differences between arrays and pointers in the form of access, and makes more essential differences between arrays and pointers in function calls, and when the arrays

It is equivalent to pointer and will be given in the next blog. If it is completely clear, it will be helpful for future programming.

 

Reference: expert c programming

Download: Expert C Programming.pdf

 

Any form of reprint, please indicate the source: http://www.cnblogs.com/yanlingyin/

A fish ~ @ Blog

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.