简体   繁体   English

如何更改函数中的int数组

[英]How can I change an int array in a function

Im new with c language and im having difficult while trying to change an array on my main function using another function. 我是C语言的新手,尝试使用其他函数更改我的主要函数的数组时遇到困难。 Besides seeing the solution I'd also like to get a full explanation what is wrong with my code and what's the explanation to your solution. 除了查看解决方案之外,我还想获得完整的解释,我的代码有什么问题以及您的解决方案的解释是什么。

OK so I did too much tryouts and error experimentation but with no success to solve my problem. 好的,所以我进行了过多的测试和错误实验,但未能成功解决问题。 Eventually this is my current code: 最终这是我当前的代码:

#include <stdio.h>
#define MAX 20
typedef int Values[MAX];

int changeArr(Values *vals2) 
{
    *vals2[0] = 200;
    *vals2[1] = 100;
    printf("%d and ", *vals2[0]);
    printf("%d\n", *vals2[1]);
    return 0;
}   

int main (int argc, char *argv[]) 
{
    Values vals;
    changeArr(&vals);
    printf("%d and ", vals[0]);
    printf("%d\n", vals[1]);
    return 0;
}

Output is: 输出为:

200 and 100
200 and 0

Instead of: 代替:

200 and 100
200 and 100

Version 1: 版本1:

#include <stdio.h>
#define MAX 20
typedef int Values[MAX];

int changeArr(int vals2[]) {
    vals2[0] = 200;
    vals2[1] = 100;
    printf("%d and ", vals2[0]);
    printf("%d\n", vals2[1]);
    return 0;
}   

int main (int argc, char *argv[]) {

    Values vals;
    changeArr(vals);
    printf("%d and ", vals[0]);
    printf("%d\n", vals[1]);
    return 0;

}

Instead of passing a pointer to the array, pass a pointer to the array's first element. 而不是将指针传递给数组,而是将指针传递给数组的第一个元素。

Version 2: 版本2:

#include <stdio.h>
#define MAX 20
typedef int Values[MAX];

int changeArr(Values *vals2) {
    (*vals2)[0] = 200;
    (*vals2)[1] = 100;
    printf("%d and ", (*vals2)[0]);
    printf("%d\n", (*vals2)[1]);
    return 0;
}   

int main (int argc, char *argv[]) {

    Values vals;
    changeArr(&vals);
    printf("%d and ", vals[0]);
    printf("%d\n", vals[1]);
    return 0;

}

use parentheses to compensate for the precedence. 使用括号来补偿优先级。

The problem in your code is that 您的代码中的问题是

*vals2[1]

is *(vals2[1]) , so it dereferences a pointer one unit of Values after the passed pointer. *(vals2[1]) ,因此它在传递的指针之后以Values一个单位解引用指针。 You have no accessible memory allocated there, so it's undefined behaviour. 您那里没有分配可访问的内存,因此它是未定义的行为。 In practice, it is the same as accessing 实际上,它与访问

arr[1][0]

for an

int arr[2][MAX];

But your vals is only (equivalent to) an int arr[1][MAX]; 但是您的vals只(相当于)的int arr[1][MAX]; , so you are accessing out of bounds. ,因此您无法访问。 But if nothing worse happens, *vals2[1] = 100; 但是如果没有更糟的情况发生, *vals2[1] = 100; in changeArr sets vals[MAX] in main to 100. It may overwrite something crucial, though. changeArrmain vals[MAX]设置为100。但是,它可能会覆盖一些至关重要的内容。

In int changeArr(Values *vals2) you are passing a pointer to an array of MAX int s, resp the first such array in an array of Values . int changeArr(Values *vals2)您传递了一个指向MAX int数组的指针,分别是Values数组中的第一个此类数组。 Then vals2[1] is the second array in that array of Values (which doesn't exist here), and *vals2[1] == vals2[1][0] the first int in that second array. 然后vals2[1]是该Values数组中的第二个数组(此处不存在), *vals2[1] == vals2[1][0]是第二个数组中的第一个int You want to modify elements of the first (and only) array in the pointed-to memory block, so you want to access vals2[0][1] , or equivalently (*vals2)[1] . 您想要修改指向的内存块中第一个(也是唯一一个)数组的元素,因此要访问vals2[0][1]或等效地(*vals2)[1]

A picture: 照片:

vals2                          vals2[1]
 |                               |
 v                               v
|vals[0]|vals[1]|...|vals[MAX-1]|x

in changeArr , the pointer vals2 points to the array vals . changeArr ,指针vals2指向数组vals Since it's a pointer to an int[MAX] , vals2+1 points to an int[MAX] at an offset of MAX*sizeof(int) bytes after the start of vals (which is just behind the end of vals ). 由于它是一个指向int[MAX] vals2+1指向的int[MAX]在一个偏移MAX*sizeof(int)字节开始后vals (它是仅次于的端vals )。 vals2[1] , or equivalently *(vals2 + 1) , is that array just after vals (which doesn't exist). vals2[1]或等效的*(vals2 + 1)vals之后的那个数组(不存在)。

You want to change vals[1] , which is located in the array vals2[0] , at an offset of 1*sizeof(int) bytes, so you need vals2[0][1] or equivalently (*vals2)[1] . 您想要更改vals[1] (位于数组vals2[0] ,其偏移量为1*sizeof(int)个字节,因此您需要vals2[0][1]或等效地(*vals2)[1]

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

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