1

What am I doing wrong ??? It is giving me an error of segmentation fault . I don't know what memory i'm accessing ??

#include <iostream>
using namespace std;

int main() {
    int t;
    int n;
    int arr[n];
    
    cin>>t;
    cin>>n;

    // taking array elements from user
    for(int i=0; i<n;i++)
    {
        cin>>arr[i];
    }
    
     // reverse of the array
     for(int i=n-1; i>=0;i--)
    {
        cout<<arr[i];
    }
    //code
    return 0;
} 
Oka
  • 23,367
  • 6
  • 42
  • 53
  • 2
    `int arr[n];` but the value of `n` is undefined at that moment. I don't think C++ has VLAs anyway (the code is not C, where support for the VLA is optional). – Weather Vane Jul 05 '21 at 18:00
  • 2
    Even if you reordered things to be `int n; cin >> n; int arr[n];`, you'd _still_ be using a nonstandard extension to have a [variable length array](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). If you want a homogenous sequence container whose size is only determined at runtime, I suggest taking a look at the [std::vector](https://en.cppreference.com/w/cpp/container/vector). – Nathan Pierson Jul 05 '21 at 18:01
  • 2
    Watch out for compiler warnings - do not ignore a single one of them. – Weather Vane Jul 05 '21 at 18:02
  • FYI, remove the C language tag. The `C` language does not have `std::cin`, and doesn't allow operator or function overloading. Thus `cin >> arr[i]` is a right shift in the C language. – Thomas Matthews Jul 05 '21 at 18:11
  • Since you don't know the quantity of data at compile time use `std::vector` and the `push_back` method. – Thomas Matthews Jul 05 '21 at 18:12

3 Answers3

3
int n;

You've default initialised this variable. Thus, the value is indeterminate.

int arr[n];

Here, you use that indeterminate value. Thus, the behaviour of the program is undefined.

There are "data flow" languages where using a variable will stop execution waiting for you to initialise it later and continue. C++ isn't such language. You must initialise everything before using the value.


Besides that, n isn't a compile time constant expression. Because the size of the array variable isn't compile time conastant, the progarm is ill-formed in C++.

If you want an array to have a dynamic size, you can use dynamic storage. Simplest way to create a dynamic array is to use std::vector.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

For starters you are trying to declare a variable length array

int n;
int arr[n];

Variable length arrays is not a standard C++ feature. Moreover you are using an uninitialized variable n as the size of the array.

Either declare the array with an expected maximum size or use standard container std::vector<int>. At least you should write provided that the compiler supports variable length arrays

int t;
int n = 1;

cin>>t;
cin>>n;

if ( n < 1 ) n = 1;
int arr[n];

//...

Also you are not reversing an array. You are trying to output an array in the reverse order.

To reverse an array you could use standard algorithm std::reverse or you can write an appropriate loop yourself as for example

for ( int i = 0; i < n / 2; i++ )
{
    // or use std::swap( arr[i], arr[n-i-1] );
    int tmp = arr[i];
    arr[i] = arr[n-i-1];
    arr[n-i-1] = tmp;
}

and then you can output the reversed array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You are not allowed to define array with Unknown size in C++ so int arr[n]; is Wrong! and if you have to use arrays and not know the size of it , you should use dynamic array with Pointers like this : int* a = new int[n] and also Deallocate Heap memory with Delete []array_name at end of your program and if it is possible for you not use arrays It's better for you use vectors because the size of it is dynamic.

look at this with vectors :

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//by : Salar Ashgi
int main()
{
    int k;
    cout<<"Enter number of elements ?\n";
    cin>>k;
    vector<int> v;
    cout<<"--------------------\n";
    int x;
    for(int i=0;i<k;i++)
    {
        cout<<"Enter num "<<i+1<<" : ";
        cin>>x;
        v.push_back(x);
    }
    
    reverse(v.begin(),v.end());//algorithm.h
    cout<<"Reverse : \n";
    for(int i=0;i<k;i++)
    {
        cout<<v[i]<<" ";
    }
}
Salar Ashgi
  • 128
  • 2
  • 13