std::array equivalent in C

std::array constructor
std::array fill
std::array 2d
std::array vs std::vector
std::array size
c++ array
std::vector operator

I'm new to C and C++, and I've read that at least in C++ it's preferable to use std::array or std::vector when using vectors and arrays, specially when passing these into a function.

In my research I found the following, which makes sense. I suppose using std::vector would fix the problem of indexing outside of the variable's scope.

void foo(int arr[10]) { arr[9] = 0; }

void bar() {
    int data[] = {1, 2};

The above code is wrong but the compiler thinks everything is fine and issues no warning about the buffer overrun.

Instead use std::array or std::vector, which have consistent value semantics and lack any 'special' behavior that produces errors like the above.

(answer from bames53, thanks btw!)

What I want to code is

float foo(int X, int Y, int l){
    // X and Y are arrays of length l
    float z[l];
    for (int i = 0; i < l; i ++){
        z[i] = X[i]+Y[i];
    return z;

int bar(){
    int l = 100;
    int X[l];
    int Y[l];
    float z[l];
    z = foo(X,Y,l);
    return 0;

I want this to be coded in C, so my question is is there a std::vector construct for C? I couldn't find anything on that.

Thanks in advance, also please excuse my coding (I'm green as grass in C and C++)

Standard C has nothing like std::vector or other container structures. All you get is built-in arrays and malloc.

I suppose using std::vector would fix the problem of indexing outside of the variable's scope.

You might think so, but you'd be wrong: Indexing outside of the bounds of a std::vector is just as bad as with a built-in array. The operator[] of std::vector doesn't do any bounds checking either (or at least it is not guaranteed to). If you want your index operations checked, you need to use instead of arr[i].

Also note that code like

float z[l];
return z;

is wrong because there are no array values in C (or C++, for that matter). When you try to get the value of an array, you actually get a pointer to its first element. But that first element (and all other elements, and the whole array) is destroyed when the function returns, so this is a classic use-after-free bug: The caller gets a dangling pointer to an object that doesn't exist anymore.

The customary C solution is to have the caller deal with memory allocation and pass an output parameter that the function just writes to:

void foo(float *z, const int *X, const int *Y, int l){
    // X and Y are arrays of length l
    for (int i = 0; i < l; i ++){
        z[i] = X[i]+Y[i];

That said, there are some libraries that provide dynamic data structures for C, but they necessarily look and feel very different from C++ and std::vector (e.g. I know about GLib).

std::array -, if std::three_way_comparable_with<E, E> is satisfied, equivalent to lhs <=> rhs;; otherwise, if comparing two const E lvalues by operator< is  7) Compares the contents of lhs and rhs lexicographically. The comparison is performed as if by calling std::lexicographical_compare_three_way on two arrays with a function object performing synthesized three-way comparison (see below).

operator==,!=,<,<=,>,>=,<=>(std::array , Where std::vector represented dynamically sized arrays, std::array is a container the array size, providing bounds checking, and allowing the use of C++ But you can only use the '=' operator on arrays of equivalent size. For a non-empty container c, the expression c. back is equivalent to * std:: prev (c. end ()) [ edit ] Example The following code uses back to display the last element of a std:: array < char > :

The closest equivalent of std::array in c is probably a preprocessor macro defintion like

#define ARRAY(type,name,length) \
     type name[(length)]

An Introduction to std::array, Our built-in multi-dimensional array is very simple to create: int marr[5][20];. This is much prettier than the equivalent std::  Introduced in C++11, std::array provides fixed array functionality that won’t decay when passed into a function. std::array is defined in the <array> header, inside the std namespace. Declaring a std::array variable is easy: #include <array> std::array<int, 3> myArray; // declare an integer array with length 3.

Ditch Those Built-in Arrays for C++ Containers, Since it is becoming increasingly common to use std::vector instead of arrays in C​++, we give examples of both conversions below. (C# to C++ Converter converts​  The C++ equivalent of a dynamically sized array would be std::vector. The C++ equivalent of a fixed size array would be std::array or std::tr1::array pre-C++11. If your vector code has no re-sizings it is hard to see how it could be significantly slower than using a dynamically allocated C array, provided you compile with some optimization enabled.

C++ Equivalent to C# Arrays, STD::array in C++ · Which C++ libraries are useful for competitive programming? Generate an array of given size with equal count and sum of odd and even  For a container c, the expression c. front is equivalent to * c. begin (). [ edit ] Example The following code uses front to display the first element of a std:: array < char , 6 > :

STD::array in C++, template <class T, size_T N> bool operator== ( const array<T,N>& lhs, const 40​, 30, 20, 10}; if (a==b) std::cout << "a and b are equal\n" ; if (b!=c) std::cout << "b  op must not have side effects. (until C++11) op must not invalidate any iterators, including the end iterators, or modify any elements of the ranges involved. (since C++11)

  • >is there a std::vector construct for C? Nope, you would have to implement it yourself or use a library for it. C has no containers included with it except the arrays and pointers.
  • C or C++ standards don't allow variable based allocated arrays on the stack. With c you'll need to go for some malloc() solution, in c++ you'll use std::vector.
  • Is there a reason that you seek for std::vector-like thing in C? Why don't you use C++ instead? If you're a beginner, you definitely want to learn C++, not C. You should only go with C, if you have a strong reason for that.
  • Your code is too mangled to understand even as pseudo code, you pass an array to an int and then comment that the int denotes size? Anyway, there is no std::vector analogue in standard C.
  • @πάνταῥεῖ actually variable-length arrays are supported in C99 (and optional in C11). So you don't need malloc there for small enough arrays.
  • Thanks! I was just coding this and I realised what you're saying about passing the output parameter to the function. I just changed it and had Y be the output. (i.e. Y[i] = X[i] + Y[i]). From the responses to my question I also gather I should probably just code in C++ for my extensions if anything. Still was an interesting excercise. Thanks again!
  • I see... I don't mean to be disrespectful with my ignorance. I really appreciate all the feedback and I will learn from it. I'll also study this code. Thanks!!
  • @Storyteller: I no longer write new. For delete, I clean it the same as created
  • What is the point of re-writing a Vector class in C++? When I first scanned the code I thought it was for C which makes for sense. Then I saw the C++ and thought, WTF?
  • @jww The question is about exposing a vector class in C
  • Here was the question: "I want this to be coded in C, so my question is is there a std::vector construct for C?" He is working in C, and he needs the Vector-like data type with the external functions like CreateVector, DestroyVector and probably some setters and getters. None of the other stuff makes sense.