Address symbol & is used in C language to: obtain the address of a variable and return a pointer value pointing to the memory location of the variable. By dereferencing a pointer, you can access the variable pointed to by the pointer. Pass the variable address so that the function can modify the variable outside the function. The address of an array element can be expressed as the array name plus an offset.
The role of the address character in C language
The address character (&) is a very important character in C language Important operator, it is used to get the address of a variable. The address of a variable is a pointer value that points to the memory location of the variable.
The function of the address character:
<code class="c">int x = 10; int *ptr = &x;</code>
At this time, the value of ptr will be the address of the x variable.
<code class="c">int y = *ptr;</code>
At this time, y will get the value of the x variable (10).
<code class="c">void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; }</code>
This function exchanges the values of two variables by getting the addresses of the a and b variables.
<code class="c">int arr[] = {1, 2, 3}; int *ptr = &arr[1];</code>
At this time, ptr will point to the second element of the array arr.
Notes on use:
You need to pay attention to the following aspects when using address characters:
The above is the detailed content of The function of address characters in C language. For more information, please follow other related articles on the PHP Chinese website!