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!
Your program is doing something completely unintended.
int *x[5] = { 1,5,4,3,1 };
int *y[5]= { 1,5,4,3,1 };
Here you're initializing a bunch of int pointers with values from 1 to 5. So they point to invalid memory.
Then later, here:
for (int z = 0; z <= 4; ++z) {
printf_s("%s", x[z]);
}
You are telling it to print the string at that invalid memory.
The 0x00000001
in Access violation writing location 0x00000001
is actually the hex representation of the first 1
in int *x[5] = { 1,5,4,3,1 };
.
What you probably want is this:
int changeArray(int *a) {
for (int z = 0; z < 5; ++z) {
a[z] = 1;
}
}
And this:
int main(void) {
int x[5] = { 1,5,4,3,1 };
changeArray(x);
for (int z = 0; z <= 4; ++z) {
printf("%d", x[z]); // also consider adding space, such as "%d "
}
}
int *x[5]
should be int x[5]
.int y[5]
at all.int changeArray(int **a)
should be void changeArray(int (*a)[5])
.free(x);
is
wrong, x
is on the stack and must not be freed.printf_s("%s", x[z]);
should be printf("%d ", x[z]);
, x[z]
is an int
and so it needs %d
as format specifier. Also note the space after it to see the different numbers and not only a large number.Here is your corrected code https://ideone.com/kwXrFg
The code should be like this:
#include <stdio.h>
#include <stdlib.h>
void changeArray(int (*a)[5]);
int main(void) {
int x[5] = { 1,5,4,3,1 };
changeArray(&x);
for (int z = 0; z <= 4; ++z) {
printf_s("%d", x[z]);
}
return 0;
}
void changeArray(int (*a)[5]) {
for (int z = 0; z < 5; ++z) {
(*a)[z] = 1;
}
}
and give output:
11111
as you can see in the Live Demo.
Here are the changes I made:
int *x[5] = { 1,5,4,3,1 };
to int x[5] = { 1,5,4,3,1 };
.y
, since you do not use it.void changeArray(int
(*a)[5]);
, since you don't return anything, and the parameter is
also changed, to pass array x
as is with the changes now.%d
to print integers, not %s
.free(x)
, since you don't dynamic allocate memory for array
x
, thus you must not de-allocate it manually.#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]);
}
}
int changeArray(int *a) {
for (int z = 0; z < 5; ++z) {
a[z] = 1;
}
}
User contributions licensed under CC BY-SA 3.0