Changing elements of an array by passing by reference to a function

1

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!

c
arrays
element
pass-by-reference
asked on Stack Overflow Oct 25, 2018 by Ton • edited Oct 25, 2018 by user3386109

4 Answers

3

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 "
    }
}
answered on Stack Overflow Oct 25, 2018 by Blaze
1
  • int *x[5] should be int x[5].
  • You do not need int y[5] at all.
  • int changeArray(int **a) should be void changeArray(int (*a)[5]).
  • You pass a pointer to an array and return nothing. 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

answered on Stack Overflow Oct 25, 2018 by mch • edited Oct 25, 2018 by mch
1

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:

  • Change int *x[5] = { 1,5,4,3,1 }; to int x[5] = { 1,5,4,3,1 };.
  • Remove y, since you do not use it.
  • Change the prototype of your function to: 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.
  • Use %d to print integers, not %s.
  • Remove free(x), since you don't dynamic allocate memory for array x, thus you must not de-allocate it manually.
answered on Stack Overflow Oct 25, 2018 by gsamaras • edited Oct 25, 2018 by gsamaras
0
#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;
    }
}
answered on Stack Overflow Oct 25, 2018 by user1888149 • edited Oct 25, 2018 by mch

User contributions licensed under CC BY-SA 3.0