modifying double pointer to an integer array

895 views Asked by At

I have a pointer to an int *array, I allocated it and then pass it to a function in order to fill in the elements of the array.

void function(int **arr);

int main()
{
    int *array;
    array=calloc(4, sizeof(int));
    function(&array);
    return0;
}


void function(int **arr)
{
    int *tmp;
    tmp=calloc(4, sizeof(int));
    tmp[0]=1;
    tmp[1]=2;
    tmp[2]=3;
    tmp[3]=4;
}

I want to assign tmp to arr. How can I do it?

3

There are 3 answers

5
Patrick Roberts On BEST ANSWER

You don't need to calloc array in main, first of all. It's a pointer and all you need to do is assign tmp to it. Here's how:

void function(int **arr);

int main()
{
    int *array;
    size_t i;

    function(&array);
    // do stuff with array
    for (i = 0; i < 4; i++)
    {
        printf("%d\n", array[i]);
    }
    // then clean up
    free(array);

    return 0;
}


void function(int **arr)
{
    int *tmp;
    tmp=calloc(4, sizeof(int));
    tmp[0]=1;
    tmp[1]=2;
    tmp[2]=3;
    tmp[3]=4;

    *arr = tmp;
}
6
Vlad from Moscow On

You should not do that because in this case there will be a memory leak because you already allocated memory for pointer array and the assignment will overwrite the value stored in the pointer. Write the function simpler

void function(int **arr)
{
    int *tmp = *arr;

    tmp[0]=1;
    tmp[1]=2;
    tmp[2]=3;
    tmp[3]=4;
}

Here is a demonstrative program

#include <stdio.h>
#include <stdlib.h>

void init( int **a, size_t n, int value )
{
    int *tmp = *a;
    size_t i = 0;

    for ( ; i < n; i++ ) tmp[i] = value++;
}

void display ( int *a, size_t n )
{
    size_t i = 0;

    for ( ; i < n; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
}

int main(void)
{
    int *a;
    size_t n = 4;

    a = calloc( n, sizeof( int ) );

    init( &a, n, 0 );
    display( a, n );

    init( &a, n, 10 );
    display( a, n );

    free( a );

    return 0;
}

The program output is

0 1 2 3 
10 11 12 13 

If you need to realloc the original array in a function then this can be done the followingway

#include <stdio.h>
#include <stdlib.h>

void init( int **a, size_t n, int value )
{
    int *tmp = *a;
    size_t i = 0;

    for ( ; i < n; i++ ) tmp[i] = value++;
}

void display ( int *a, size_t n )
{
    size_t i = 0;

    for ( ; i < n; i++ ) printf( "%d ", a[i] );
    printf( "\n" );
}


void alloc_new( int **a, size_t n )
{
    int *tmp = malloc( n * sizeof( int ) );

    if ( tmp )
    {
        free( *a );
        *a = tmp;
    }   
}

int main(void)
{
    int *a;
    size_t n = 4;

    a = calloc( n, sizeof( int ) );

    init( &a, n, 0 );
    display( a, n );

    alloc_new( &a, n );

    init( &a, n, 10 );
    display( a, n );

    free( a );

    return 0;
}
2
larsr On

Perhaps you should declare function before you use it in main. The compiler may produce the wrong code if it thinks that function expects an int argument when it should be an int** argument...

Did you just add a declaration, or did i miss it? If so, sorry!