I am trying to pass an array by reference and change the values in the array in changeArray(). I am getting an error which states "Access violation writing location 0x00000001." I read Changing array inside function in C and I used Ryyker's answer to achieve my intended result (to have x[]={1,1,1,1,1]) but I get the aforementioned error. Here's my code:
#include <stdio.h>
#include <stdlib.h>
int changeArray(int **a);
int main(void) {
int *x[5] = { 1,5,4,3,1 };
int *y[5] = { 1,5,4,3,1 };
changeArray(&x);
for (int z = 0; z <= 4; ++z) {
printf_s("%s", x[z]);
}
free(x);
}
int changeArray(int **a) {
for (int z = 0; z < 5; ++z) {
(*a)[z] = 1;
}
}
I know there are similar posts, but all the ones I have seen don't seem to solve my problem, any help is appreciated!