简体   繁体   English

通过传递给函数来更改数组的元素

[英]Changing elements of an array by passing by reference to a function

I am trying to pass an array by reference and change the values in the array in changeArray(). 我试图通过引用传递数组,并在changeArray()中更改数组中的值。 I am getting an error which states "Access violation writing location 0x00000001." 我收到一条错误消息,指出“访问冲突写入位置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. 在C中阅读了在函数内部更改数组,并使用Ryyker的答案达到了预期的结果(使x [] = {1,1,1,1,1]),但是出现了上述错误。 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. 在这里,您要初始化一堆int 指针 ,其值从1到5。因此它们指向无效的内存。

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 }; 0x00000001Access violation writing location 0x00000001实际上是第一的十六进制表示法1int *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 *x[5]应该是int x[5]
  • You do not need int y[5] at all. 您根本不需要int y[5]
  • int changeArray(int **a) should be void changeArray(int (*a)[5]) . int changeArray(int **a)应该为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. 是错误的, x在堆栈上,不能释放。
  • printf_s("%s", x[z]); should be printf("%d ", x[z]); 应该是printf("%d ", x[z]); , x[z] is an int and so it needs %d as format specifier. x[z]是一个int ,因此需要%d作为格式说明符。 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 这是您的更正代码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 11111

as you can see in the Live Demo . 正如您在Live Demo中看到的那样。


Here are the changes I made: 这是我所做的更改:

  • Change int *x[5] = { 1,5,4,3,1 }; 更改int *x[5] = { 1,5,4,3,1 }; to int x[5] = { 1,5,4,3,1 }; int x[5] = { 1,5,4,3,1 }; .
  • Remove y , since you do not use it. 删除y ,因为您不使用它。
  • Change the prototype of your function to: void changeArray(int (*a)[5]); 将函数的原型更改为: 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. ,因为您什么也不返回,并且参数也已更改,所以现在将数组x与更改一样原样传递。
  • Use %d to print integers, not %s . 使用%d打印整数,而不是%s
  • Remove free(x) , since you don't dynamic allocate memory for array x , thus you must not de-allocate it manually. 删除free(x) ,因为您没有为数组x动态分配内存,因此您不能手动取消分配内存。
#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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM