What is the error in my pointer passing to the read_vector() function?

Asked

Viewed 21 times

-1

void read_vetor(int *vet, int n) 
{
    for (int i=0;i<n;i++)
    {
        scanf_s(" %d", *(vet+i));
    }
}

void write_vetor(int* vet, int n)
{
    for (int i=0;i<n;i++)
    {
        printf("%d", *(vet+i));
    }
}

void main()
{
    int a[4], b[4];

    printf("Put values for a[4]:    ");
    read_vetor(a, 4);
    printf("Put values for b[4]:    ");
    read_vetor(b, 4);
    write_vetor(a, 4);
    write_vetor(b, 4);

}

My error happens in the read_vector function, when I’m passing the pointer, but I can’t see why. Someone can help me?

1 answer

0

Variables should not be passed to function scanf by value. The correct form would be to pass the address of the variable.

scanf_s(" %d", vet + i);
  • Thank you for the reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.