C Collections

cvector · Section 3

cvec_at_ptr

cvec_at_ptr - access element at index (type-inferred, returns pointer or NULL)

C Collections (libccollections, -lccollections)

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

Type-inferred wrapper for cvector_at() that gives a pointer to the
element, already of the right type, or NULL when the index is out of
bounds. Unlike cvec_at, it doesn't stop the program for such an index.

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

A pointer to the element at the index, or NULL when the index is out of
bounds.

An index out of bounds gives NULL and doesn't stop the program.
 
The pointer is valid only until the size of the vector changes.

cvec_construct(vec, int);
cvec_push(vec, 42);
int *ptr = cvec_at_ptr(vec, 0);
if (ptr) {
  *ptr = 5; // Change the element in place
}
cvec_destroy(vec);

cvector_at(3), cvec_at(3)