The left and right values in C. And compare the differences between array names and pointers for array elements.

Source: Internet
Author: User

The left and right values in C. And compare the differences between array names and pointers for array elements.

Left value: the symbol that appears on the left of the value assignment is sometimes called the left value.

Right value: the symbol that appears on the right of the value assignment is sometimes called the right value.

The compiler assigns an address (left value) to each variable. This address is known during compilation, and the variable remains at this address during runtime. On the contrary, the value stored in the variable (its right value) is only known at runtime. If you need to use the value stored in the variable, the compiler sends a command to read the variable value from the specified address and store it in the register.

We can see that the address of each symbol is known during compilation.

Compare the following statements:

// Common variables

Int a = 1; // here a appears as the left value, representing the address, that is, the memory address represented by a is saved to the value 1. That is, the data or value of the location where a represents the memory address is 1.

Int B = a; // here, a appears as the right value, representing the value, that is, the data on the memory address indicated by a, that is, the value 1.

// Pointer

Int * p = a; // here p appears as the left value, representing the address. Think of the left side as a whole, which is equivalent to storing the value of a in the place where (* p) represents the memory address, while p is the address represented by (* p. P, As the left value, represents a memory address. The value stored in the memory address (* p) is a value, but this value indicates the memory address. Then, where (* p) represents the memory address, the value is stored, that is, the value of.

Int * pb = p; // here p appears as the right value, representing a value. That is to say, the value of p in the memory address is assigned to the pb memory address, so the data on the pb and p memory addresses is the same, that is, pointing to the same place.

// Char array

Char arr [] = "abcdefg"; // here, arr appears as the left value, representing the address. Because the symbol itself acts as the left value, it represents a memory address. Therefore, when an array is used to retrieve elements, it is equivalent to directly referencing the memory.

Printf ("% s", arr); // here, arr appears as the right value, representing a value, that is, the entire string.

Compare the array name and pointer to get elements. The array name is a memory address. If the compiler needs an address (which may also require an offset) to execute an operation, it can directly perform the operation without adding the instruction to obtain the specific address first. On the contrary, a pointer must first obtain its current value at runtime before it can be unbound from reference. An array is a direct reference to memory, and a pointer is an indirect reference to memory.

In this way, you can understand why the following code is incorrect:

// File1

Int a [10];

// File2

Extern int *;

That is, file 2 wants to use some variables defined in file 1. However, the above Code is incorrect. Because file 1 declares a as an array, in fact it accesses the memory directly by referencing it. In file 2, the external declaration of a is a pointer. When a [I] is used to retrieve elements, because file 2 sees a as a pointer, it indirectly references the memory, the data of address a is obtained first, and the array is used as the address before the data is retrieved. This is of course incorrect.

The following is some test code:

We can see that the array name is printed by % # x in the printf function as the right value. Print by % # x. The output addresses of ga and & ga are the same. (In my understanding, the ga array name itself represents the memory address. When it appears as the right value, it represents a string, but it is printed by address here; & ga is the address where the symbol ga is located. -- The explanation is not very good .)

However, if the pointer is printed by % # x, the pointer name indicates the value of the address (that is, the address pointing to the place, because the pointer appears as the right value and the value is used ); the address pointer name, that is, the & pointer name. Printed is the address represented by the pointer name, that is, the address represented by the pointer symbol itself.

# Include
 
  
# Include
  
   
Void my_array_func (char ca []); void my_pointer_func (char * pa); char ga [] = "abcdefghijklmn"; int main () {printf ("ga = % # x \ n", ga); printf ("addr of global array & ga = % # x \ n", & ga ); // printf ("& (& ga) =%# x \ n", & (& ga); // (leftmost) & it must be printf ("ga c = % c \ n", ga); printf ("ga s = % s \ n", ga ); printf ("addr (ga [0]) & (ga [0]) =%# x \ n", & (ga [0]); printf ("addr (ga [1]) & (ga [1]) = % # x \ n", & (ga [1]); my_array_func (ga); my_pointer_func (ga); system ("pause"); return 0;} void my_array_func (char ca [10]) {printf ("addr of array param & ca =%# x \ n", & ca); printf ("addr (ca [0]) & (ca [0]) = % # x \ n ", & (ca [0]); printf (" addr (ca [1]) & (ca [1]) = % # x \ n ", & (ca [1]); printf (" ca = % # x \ n ", ca ); printf ("++ ca =%# x \ n", ++ ca);} void my_pointer_func (char * pa) {printf ("addr of ptr param & pa =%# x \ n", & pa); printf ("addr (pa [0]) & (pa [0]) = % # x \ n ", & (pa [0]); printf (" addr (pa [1]) & (pa [1]) = % # x \ n ", & (pa [1]); printf (" pa = % # x \ n ", pa ); printf ("++ pa =%# x \ n", ++ pa );}
  
 
Result:




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.