I’m trying to run sieve function and it should return a vector of prime numbers, there’s a problem with the 18th line in code . 18th line isn’t working, it feels as if it’s ignored by the compiler. It doesn’t cause an error neither is it pushing the number in to the vector. This is my code:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
vector<int>* sieve(int n){
bool arr[n];
memset(arr, true, sizeof(arr));
for(int i=2;i*i<n;i++){
if(arr[i]){
for(int j=i*i;j<n;j+=i){
arr[j]=false;
}
}
}
vector<int>* primes;
for(int i=2;i<n;i++){
if(arr[i]){
(*primes).pb(i);//PROBLEM IN THIS LINE
}
}
return primes;
}
int main(){
vector<int>* primes = sieve();
for(int el:*primes){
cout<<el<<endl;
}
return 0;
}
I’ve marked the 18th line with a comment //PROBLEM IN THIS LINE