The role of square brackets in C
The square brackets in C have two main uses:
1. Array index
Square brackets are used to access elements in an array. An array is a fixed-size collection of data of the same type, with each element having a unique integer identifier called an index. The index within square brackets specifies the array element to be accessed.
For example:
<code class="cpp">int myArray[5]; myArray[0] = 10;</code>
This line of code defines an array myArray
containing 5 integer elements. It assigns the value 10 to the first element of the array (index 0).
2. Pointer dereference
Square brackets can also be used to dereference pointers. A pointer is a variable that stores the address of another variable. The expression within square brackets is used to obtain the value of the variable pointed to by the pointer.
For example:
<code class="cpp">int* myPointer = &myVariable; int dereferencedValue = *myPointer;</code>
This line of code defines a pointer myPointer
, which points to the variable myVariable
. The expression inside square brackets *myPointer
dereferences the pointer and returns the value of myVariable
, storing it in the variable dereferencedValue
.
The above is the detailed content of The function of brackets in c++. For more information, please follow other related articles on the PHP Chinese website!