Passing int* as parameter [duplicate]

  arrays, c++, pointers

I am trying to make a quick program that checks if the elements of an array can represent a premutation. The logic I use could be improved but my main problem is that v is not loaded correctly(I only get 1s).
my code“`

#include <iostream>

using namespace std;

int* readPerm(int n) {
    int v[n];
    int *perm=v;
    for(int i=0;i<n;i++) {
        cin>>perm[i];
    }


    return perm;
}

void isPermutation(int n,int *p) {
    int v[n];
    for(int i=0;i<n;i++) {

        v[i]=p[i];
    }

    for(int i=0;i<n;i++) {
        if(v[i]!=1 || v[i]>n || v[i]<n) {
            cout<<"is not a permutation";
            return;
        }
    }
    cout<<"is a permutation";
}

int main() {
    cout<<"How many elements in the permutation?";
    int n;
    cin>>n;
    int *p=readPerm(n);
    isPermutation(n,p);
    return 0;
}

Source: Windows Questions C++

LEAVE A COMMENT