简体   繁体   English

作为 function 参数的数组指针

[英]a pointer of array as function parameter

I have an array that I created with malloc.我有一个用 malloc 创建的数组。

int *array = (int *) malloc(10 * sizeof(int));

I want to use this array in function.And I want to affect values of the array in main function. Like a pointer我想在 function 中使用这个数组。我想影响主 function 中数组的值。就像一个指针

void func(int **array);

How can I do this correctly?我怎样才能正确地做到这一点?

First thing that comes to mind(working):首先想到的(工作):

void func(int **arr){
    arr[0][0] = 100;
    arr[0][1] = 200;
}

int main(){
    int *arr = (int*) malloc(10 * sizeof(int));

    func(&arr);

    printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);

    return 0;
}

Second(Not work):第二(不工作):

typedef int* intarr;

void func(intarr *arr){
    *arr[0] = 100;
    *arr[1] = 200;
}
int main(){
    intarr arr = (int*) malloc(10 * sizeof(int));

    func(&arr);

    printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);

    return 0;
}

My first question is why am I getting a segmentation error in the second method?我的第一个问题是为什么我在第二种方法中遇到分段错误?

My second question is how accurate is the first method?我的第二个问题是第一种方法有多准确?

My third question is what are you using?我的第三个问题是你在用什么?

You only need to use **arr if the function needs to be able to reassign the caller's variable.如果 function 需要能够重新分配调用者的变量,则只需使用**arr See Changing address contained by pointer using function for examples where this is needed.有关需要此操作的示例,请参阅使用 function 更改指针包含的地址

If it's just using and/or updating the contents of the array, just pass the pointer itself.如果它只是使用和/或更新数组的内容,只需传递指针本身。

And if the function needs to know the size of the array, you'll need to pass that explicitly as well.如果 function 需要知道数组的大小,您也需要显式传递它。 See How to find the 'sizeof' (a pointer pointing to an array)?请参阅如何找到“sizeof”(指向数组的指针)?

void func(int *arr){
    arr[0] = 100;
    arr[1] = 200;
}

int main(){
    int *arr = malloc(10 * sizeof(int));

    func(arr);

    printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);

    return 0;
}

Then, there's this:然后,就是这样:

#include <stdio.h>

#if 1
// Showing the equivalent to "int *arr", array notation aids consistency
void func( int arr[], int sz ) {
    // Can test that array index does overrun size of array
    arr[0] = 100;
    arr[1] = 200;
}
#else
void func( int *arr, int sz ) {
    *(arr + 0) = 100;
    *(arr + 1) = 200;
}
#endif

int main() {
// EDIT - switch to calloc
//  int *arr = malloc( 10 * sizeof *arr ); // better. simple. don't cast.
    int *arr = calloc( 10, sizeof *arr ); // consistent initialisation
    /* omitting test for failure */

    func( arr, 10 ); // pass the size because function won't know.

    printf("arr[0] = %d\narr[1] = %d\n", arr[0], arr[1]);

    // No one has mentioned "memory leaks".
    // Amazing what qualifies as a "good" answer.

    free( arr );

    return 0;
}

People read code, too.人们也阅读代码。 Consideration for their ease is as important as getting the code right.考虑它们的易用性与正确编写代码同样重要。 (Bugs lurk in corners that are 'challenging'. Think about off-by-one mistakes.) (错误潜伏在“具有挑战性”的角落。想想失误。)

By using calloc() , the caller has assurance that array elements NOT set by the function will be consistent (ie. not changing from execution to execution as uninitialised variables can.) Consistent bad performance, if there are bugs, makes tracking down bugs much easier.通过使用calloc() ,调用者可以保证 function 未设置的数组元素将是一致的(即不会像未初始化的变量那样从执行更改到执行。)一致的糟糕性能,如果有错误,使得跟踪错误很多更轻松。 Bugs that appear for some tests, then disappear for others, will cause the coder to age at an accelerated rate.一些测试出现的错误,然后在其他测试中消失,将导致编码器加速老化。

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

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