Brackets serve multiple purposes in C: accessing array elements and dereferencing pointers. Define and access elements in vectors, and create range objects. Access elements in an associative container. Specify function parameters. Specify the array size.
Usage of square brackets in C
Usage of square brackets[]
in C There are multiple uses, depending on the context:
1. Array subscripting and pointer arithmetic:
array[i]
accesses the i
th element of array array
. *ptr[i]
dereferences the i
th element of the array pointed to by the pointer ptr
. 2. Vectors and ranges:
vector<int> v = {1, 2, 3}; v[0]
Access the first element of vector v
. auto rng = array[start:end];
creates an array range from start
to end-1
. 3. Associative containers (such as maps and collections):
map<string, int> m = {{"a", 1}}; m["a"]
Access the value whose key is "a" in map m
. 4. Function parameters:
void func(int arr[]);
Define a function that accepts an integer array as a parameter. 5. Array size:
int arr[10];
declares an integer array containing 10 elements. Other usage:
int i = (int) 3.14;
Casts the floating point number 3.14 to an integer. struct { int x; int y; } point;
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!