C Collections

cvector · Section 3

cvec_at

cvec_at - access element at index (type-inferred, returns reference)

C Collections (libccollections, -lccollections)

#include <ccollections/cvector.h>
 
element_ref = cvec_at(v, index);

Type-inferred wrapper for cvector_at(). It gives a reference to the
element, already of the right type, rather than a pointer, so you can use
it as an lvalue in an assignment.

v      Vector to access
index  Zero-based index of the element

A reference to the element at the index.

What you get is a reference, not a pointer, and you can put it on the
left side of an assignment.
 
index is evaluated exactly once, so an expression with a side effect, for
example cvec_at(v, i++), is safe to pass.
 
An index that is out of bounds stops the program with ccol__fatal_err().
If you'd rather get NULL for such an index, use cvec_at_ptr.

cvec_construct(vec, int);
cvec_push(vec, 42);
int val = cvec_at(vec, 0);  // val == 42
cvec_at(vec, 0) = 5;        // the first element is now 5
cvec_destroy(vec);

cvector_at(3), cvec_at_ptr(3)