lvalue : The symbol that appears to the left of the assignment is sometimes referred to as the left value.
rvalue : The symbol that appears to the right of the assignment is sometimes called the right value.
The compiler assigns an address (lvalue) to each variable, which is known at compile time and is stored at this address at run time. Conversely, the value stored in the variable (its right value) is known only at run time. If a value stored in a variable is needed, the compiler reads the value of the variable from the specified address and stores it in the register.
As you can see, the address of each symbol is known at compile time.
Compare several formulas:
General variables
int a=1;//Here A as Lvalue appears, which represents the address, that is, the memory address represented in a is stored in the value 1. That is, a represents the memory address of this place data or value, is 1.
int b=a;//Here A as rvalue, which represents the value, that is, the data on the memory address represented by a, that is, the value 1.
Pointer
int *p=a;//here p as an lvalue appears, which represents the address. Consider the left as a whole, equivalent to the value of a in the memory address represented by (*P), and P is the address represented by (*P). That is, p as the left value, which itself represents a memory address, the value stored in that memory address (*P)--is a numeric value, but the value represents a memory address. The value of a is then stored in the memory address (*P) represented.
int *pb=p;//here p as the right value appears, which represents the numeric value. That is, p this memory address place value to the PB memory address, so PB and P memory address on the same data, that is, pointing to the same place.
Char Array
Char arr[]= "ABCDEFG";//Here arr appears as an lvalue, which represents the address. Because the symbol itself is a left value, it represents a memory address. Therefore, using an array name to fetch an element is equivalent to a direct reference to memory.
printf ("%s", arr);//Here arr appears as an rvalue, representing the value, that is, the entire string.
compares array names and pointers to elements . The array name is a memory address. If the compiler requires an address (which may also need to be offset) to perform some action, it can operate directly, without requiring an additional instruction to first obtain a specific address. Instead, for a pointer, you must first get its current value at run time before you can dereference it. An array is a direct reference to memory, and a pointer is an indirect reference to memory.
In this case, you can understand why the following code is wrong:
File1
int a[10];
File2
extern int *a;
That is, file 2 wants to use some of the variables defined in file 1. But the above code is wrong. Because a is declared in file 1 as an array, it actually accesses the memory in a direct reference. While in the file 2, the external declaration as a pointer, in the a[i] to take the element, because the file 2 sees a is a pointer, is a memory indirect reference, the first to obtain the address of the data, and the array as an address to fetch data, which is of course wrong.
Here are some test codes:
You can see that the array name is in the printf function, and as the right value appears, if you press% #x来打印的话, the address is printed. That is, by% #x打印, GA and &ga output the same address. (My understanding is that the array name of GA represents the memory address, as the right value appears, is the string, but this is printed by address, and &ga is the address where the symbol GA resides.) --not very well explained. )
But for pointers, by% #x打印, the pointer name is the value of the address (that is, the address pointing to the place, because the pointer appears as an rvalue, the value is taken), and the pointer name, & pointer name, is the address that the pointer name represents, which is the address represented by the pointer itself.
#include <stdio.h> #include <stdlib.h>void my_array_func (char ca[]); void My_pointer_func (char *pa); char GA [] = "ABCDEFGHIJKLMN"; int main () {printf ("ga =% #x \ n", GA);p rintf ("Addr of global array &ga =% #x \ n", &ga);//pri NTF ("& (&GA) =% #x \ n", & (&ga)),//(leftmost) & must operate on Lvalue ("ga c =%c\n", GA);p rintf ("ga s =%s\n", GA) ;p rintf ("addr (Ga[0]) & (ga[0]) =% #x \ n", & (Ga[0]));p rintf ("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);p rintf ("addr (Ca[0]) & (Ca[0]) =% #x \ n ",& (Ca[0]));p rintf (" addr (Ca[1]) & (ca[1]) =% #x \ n ",& (Ca[1]));p rintf (" CA =% #x \ n ", CA);p rintf (" ++c A =% #x \ n ", ++ca);} void My_pointer_func (char *pa) {printf ("addr of ptr param &PA =% #x \ n", &pa);p rintf ("addr (Pa[0]) & (pa[0]) = % #x \ n ",& (Pa[0]));p rintf (" addr (Pa[1]) & (pa[1]) =% #x \ n ", & (pa[1]));p rintf ("pa =% #x \ n", PA);p rintf ("++pa =% #x \ n", ++PA);}
Results:
The left and right values in the C language. And the difference between array names and pointer-to-array elements.