Home > Net >  a pointer of array as function parameter
a pointer of array as function parameter

Time:10-07

I have an array that I created with 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

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?

CodePudding user response:

You only need to use **arr if the function needs to be able to reassign the caller's variable. See Changing address contained by pointer using function for examples where this is needed.

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. See How to find the 'sizeof' (a pointer pointing to an array)?

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;
}

CodePudding user response:

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]);

    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. Bugs that appear for some tests, then disappear for others, will cause the coder to age at an accelerated rate.

  • Related