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!

user3386109
  • 34,287
  • 7
  • 49
  • 68
Ton
  • 49
  • 1
  • 3
  • 5
    `int *x[5]` should be `int x[5]` (the same for `y`) and `int changeArray(int **a)` should be `int changeArray(int (*a)[5])`. – mch Oct 25 '18 at 06:41
  • Remove the * before x and y. Also, you shouldn't call `free(x)`, as the array is not heap allocated – Petter Hesselberg Oct 25 '18 at 06:42
  • With this statement you are filling address 1,5,4,3,1 in pointer x[0], x[1] ... int *x[5] = { 1,5,4,3,1 }; It is similar like int *x = 5; which is not valid better take integer type array it does not mandatory variable to be of pointer type for call by reference. – anshkun Oct 25 '18 at 07:01
  • 2
    How did you **not** get any compiler diagnostics? There are *none* mentioned in your post. Please find out where the compiler outputs the diagnostics messages! – Antti Haapala -- Слава Україні Oct 25 '18 at 07:02
  • `"Access violation writing location 0x00000001."` **Ouch!**. You are trying to access memory at Address `1` which is at the very bottom of the *System Reserved* memory pool. Not good. – David C. Rankin Oct 25 '18 at 07:11

4 Answers4

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 "
    }
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
  • 1
    Well done, there was no need to pass a pointer to array to begin with since the array had automatic storage type with its scope in `main()` to begin with... – David C. Rankin Oct 25 '18 at 07:13
  • Yeah, I think OP misundestood how pointers and arrays work in C. I can relate, it took me a good while to understand those properly. – Blaze Oct 25 '18 at 07:21
  • 1
    We all have that same bump on our foreheads from beating our head into that same wall when we were learning `:)` – David C. Rankin Oct 25 '18 at 07:21
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

mch
  • 9,424
  • 2
  • 28
  • 42
  • Nice answer mch, but it's not like `x` doesn't need to be freed, it must not be freed, as I mention in my answer. – gsamaras Oct 25 '18 at 06:54
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.
gsamaras
  • 71,951
  • 46
  • 188
  • 305
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;
    }
}
mch
  • 9,424
  • 2
  • 28
  • 42
user1888149
  • 143
  • 1
  • 4