The square brackets in C are used to: 1. Define and operate arrays, vectors, and character arrays; 2. Create pointer arrays; 3. Use range operators to specify ranges; 4. Access arrays as subscript operators or vector elements; 5. Define an anonymous structure or union.
Square brackets in C: meaning and purpose
In the C programming language, square brackets ([] ) has many uses:
1. Arrays and vectors
Syntax:
<code class="cpp">int array[size]; // 定义一个大小为 size 的数组 vector<int> vec; // 定义一个 vector</code>
2. Pointer array
Syntax:
<code class="cpp">int *ptr[size]; // 定义一个指向 int 类型指针的数组</code>
3. Character array
Syntax:
<code class="cpp">char str[] = "Hello"; // 定义一个字符串</code>
4. Range operator
Syntax:
<code class="cpp">vec[start:end]; // 获取一个向量从 start 到 end-1 的子范围</code>
5. Subscript operator
Syntax:
<code class="cpp">array[index]; // 访问数组 array 中索引为 index 的元素</code>
6. Other uses
The above is the detailed content of What do square brackets mean in c++. For more information, please follow other related articles on the PHP Chinese website!