I created an array of size N
, where N
is a variable and not initialized. arr[N]
is of variable size. It should give an error but it runs fine.
#include<iostream>
using namespace std;
int time_calculator(int ,int *,int );
int main(){
int N,RN,i;
int arr[N];
cin>>N;
cin>>RN;
for(i=0; i<N ; i++)
cin>>arr[i];
int time=time_calculator(N,arr,RN);
cout<<"time required= "<<time<<" sec";
return 0;
}
int time_calculator(int n, int * s, int rn){
int sm=*s;
for(int i=0;i<n;i++)
if(sm>*(s+i)){
int t=sm;
sm=*(s+i);
*(s+i)=t;
}
return rn-sm;
}
Here I created an array of variable size, but the code runs fine.
The array is not created dynamically. sm
is a variable for the smallest element of arr
, initialized by arr[0]
. s
is a pointer to arr
. Please tell me why an error is not thrown.