Store size of array is not known

I want to create the program to reverse the array. But I am getting an error that storage size of array a[] is not known.

#include<iostream>
using namespace std;

int main()
{
    int a[];
    int b, c;
    cin >> b;
    for (int i = 0; i < b; i++)
        cin >> a[i];
    for (c = b; c >= 0; c--)
        cout << a[c] << endl;

    return 0;
}

i want to create the program to reverse the array

Simply use std::vector for that.

A good starting would be:

#include <iostream>
#include <vector>

int main()
{
    std::size_t size;
    std::cin >> size;        // size of the array    
    std::vector<int> a(size);// allocate memory for provided size and 
                             // initialize with 0's
    // using range based loop iterate though referances of
    // each elements in the array and update with user input.
    for (int& element : a) std::cin >> element;

    return 0;
}

C/C++ program to find the size of int, float, double and char; Generate an array of given size with equal count and sum of odd and even numbers; size of char datatype and char array in C; Get the stack size and set the stack size of thread attribute in C; Find elements of an Array which are Odd and Even using STL in C++

Extension to JeJo's answer:

The latter is simple, elegant and efficient – for any type similarly simple as int. If you are dealing with more complex types, though, then with this approach you'd first default-initialise all elements and then copy- or move-assign the final objects, which you would most likely want to avoid. In such a case, the following approach is superior:

std::vector<SomeComplexType> v;
v.reserve(NumberOfObjectsYouNeed);

// appropriate loop definition here!
{
    v.emplace_back(/* the parameters you want to/must provide to constructor */);
}

Java Array Size. In Java, Array Objects does not use a method to determine it's size. The right way to do it is documented in the Java Specification. It mentions that a Java Array has a property declared as public final length; Since we can't change the size of an array when created, the value of this property does not change in the lifetime of the array object.

You are not defining a size for the a[] array, thus the error message. Arrays must have a size specified. In your case, you need to use the new[] operator to allocate the array after you determine that size from the user, eg:

#include <iostream>

int main() {
    int *a;
    int b;

    std::cin >> b;
    a = new int[b];

    for(int i = 0; i < b; ++i)
        std::cin >> a[i];

    for(int c = b - 1; c >= 0; --c)
        std::cout << a[c] << std::endl;

    delete[] a;
    return 0;
}

However, the preferred way to use a dynamically sized array in C++ is to use the standard std::vector container instead, eg:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

int main() {
    std::vector<int> a;
    int b;

    std::cin >> b;
    a.reserve(b);

    std::copy_n(
        std::istream_iterator<int>(std::cin), n,
        std::back_inserter(a)
    );

    std::for_each(a.rbegin(), a.rend(),
        [](int i){ std::cout << i << std::endl; }
    );

    /* alternatively:
    std::reverse(a.begin(), a.end());
    for(int i : a)
        std::cout << i << std::endl;
    */

    return 0;
}

Output: 0 0 0 0 on a IA-32 machine.. The function fun() receives an array parameter arr[] and tries to find out number of elements in arr[] using sizeof operator.

You need to must assign a maximum size of the array Just try this by assigning a length of an array Ex Int a[5];

Do fixed-size arrays not support .size()? Or, am I doing smthg wrong below? Running irun 13.1, I am told that .size() is not a valid built in method name for this object. If they do not, is this b/c a. the expectation is that someone used a parameter/constant to specify the size of the array and

Array size, returned as a row vector of nonnegative integers. Each element of sz represents the length of the corresponding dimension of A . If any element of sz is equal to 0 , then A is an empty array.

Returns the number of elements in the array container. The size of an array object is always equal to the second template parameter used to instantiate the array template class (N). Unlike the language operator sizeof, which returns the size in bytes, this member function returns the size of the array in terms of number of elements. Parameters none

I have to take a input either through C or C++ program in which I don't know the number of inputs given by user. All the numbers are number, but I only know that to read 'n' number of characters we run a loop from i to n and read them in an array of 'n' length.

Comments
  • What's unclear about the error message? You didn't specify a size for the array.
  • @ShivamSharma std::vector. Did you find that as part of your research?
  • @ShivamSharma Here's a list we keep.
  • This is asked every day. Every, single, day. Please do some research before posting!
  • for(c=b ... b is out of bounds. You need c=b-1 in this situation.
  • Not that simple - you impose a maximum length for the array this way, and user might input a larger number! So you then need to check this case, too, and provide some appropriate error handling.
  • Look at the answers using a std::vector instead.
  • @Aconcagua can u suggest me a good programming book to learn programming in c++ or any online free course?
  • @ShivamSharma Have a look at here.