Home > Software engineering >  How to pass pointer of arrays to functions
How to pass pointer of arrays to functions

Time:10-14

With my programm I try to change the order of numbers in the int array. To the first function, I just passed both arrays and printed the array called arraytemp with the changed order. After that I printed in the main function the same array, just to see if the array was filled too. I havented used any pointers in the first function - how did the array got filled? Does the arrays adress get passed to functions anyway?

Then I wanted to pass arrays with the same content to the second function, but this time I used pointers. I have no clue, how to get the same result printed, because I get a stack smashing error. I am kinda comfused with '*' and '&'. So, how should I pass these arrays when using pointers?

#include <stdio.h>

void switchnum (int arraytemp[6], int array[], int laenge) {
    printf("\n\nAfter (in function 1):\n");
    for(int i = 0 ; i<laenge ; i  ) {
        arraytemp[i] = array[laenge-1-i];
        printf("%d ", arraytemp[i]);
    }
    return 0;
}

void switchnum2 (int *arraytemp2[6], int array2[], int laenge2) {
    printf("\nAfter (in function2):\n");
    for(int j = 0 ; j<laenge2 ; j  ) {
        arraytemp2[j] = array2[laenge2-1-j];
        printf("%d ", arraytemp2[j]);
    }
    return 0;
}
int main() {
    int array[] = {4,8,1,3,0,9};
    int arraytemp[6];
    printf("Before (main):\n");
    for(int i = 0 ; i<6 ; i  ) {
        printf("%d ", array[i]);
    }  
    
    switchnum(arraytemp, array, 6);

    printf("\nAfter (in main):\n");
    for(int i = 0 ; i<6 ; i  ) {
        printf("%d ", arraytemp[i]);
    }   
    
    
    int array2[] = {4,8,1,3,0,9};
    int arraytemp2[6];
    
    switchnum2(arraytemp2, array2, 6);    
    
    return 0;
}

CodePudding user response:

The compiler adjusts a parameter having an array type to pointer to the array element type.

So this function declaration

void switchnum (int arraytemp[6], int array[], int laenge);

is equivalent to the following declaration

void switchnum (int arraytemp[], int array[], int laenge);

and the same way is equivalent to the following declaration

void switchnum (int *arraytemp, int *array, int laenge);

As for this function declaration

void switchnum2 (int *arraytemp2[6], int array2[], int laenge2);

then it is adjusted by the compiler to the declaration

void switchnum2 (int **arraytemp2, int *array2, int laenge2);

So the used argument expression and the function parameter have incompatible pointer types.

Pay attention to that in this call

switchnum(arraytemp, array, 6);

the both arrays are converted to pointers to their first elements of the type int *.

In fact this call is equivalent to

switchnum( &arraytemp[0], &array[0], 6);

CodePudding user response:

How to pass pointer of arrays to functions

In this case you are trying to pass an array of pointers to a function and not a pointer of arrays:

void switchnum2(
    int* arraytemp2[6], int array2[], int laenge2)

Maybe it helps to see the output of this example program, as it shows the output of many of your cases

Example

#include <stdio.h>

void test_arr(int*[6]);

int main(void)
{
    int array[] = {4, 8, 1, 3, 0, 9};

    int* pArr[6] = {0};  // 6 pointers to int

    printf("original vector in main(): ");
    for (int i = 0; i < 6; i  = 1) printf("%d ", array[i]);
    printf("\n");

    for (int i = 0; i < 6; i  ) pArr[i] = &array[i];
    test_arr(pArr);

    printf("\nIn main() &array[0] = %p\n", &array[0]);

    return 0;
}

void test_arr(int* pInt[6])
{
    printf("In test_array(): ");
    for (int i = 0; i < 6; i  = 1)
        printf("%d ", *pInt[i]);
    printf("\n");

    int* myP = *pInt;
    printf("*pInt\tpoints to value %d\n", *myP);

    myP = pInt[0];
    printf("pInt[0]\tpoints to value %d\n", *myP);

    int x = *pInt[0];
    printf("*pInt[0] =  %d\n", x);

    printf("\ntest_array() &pInt[0] = %X\n", pInt[0]);

    return;
}

output

original vector in main(): 4 8 1 3 0 9
In test_array(): 4 8 1 3 0 9
*pInt   points to value 4
pInt[0] points to value 4
*pInt[0] =  4

test_array() &pInt[0] = 197BFB00

In main() &array[0] = 000000CF197BFB00

Your program with some changes in the functions

I changed some lines in your code to get the expected result

#include <stdio.h>
void switch1(const int[],int[],const int);
void switch2(const int[],int*[],const int);
void show_array(const int[6],const char*);


int main(void)
{
    int arr_out[] = {0,0,0,0,0,0};
    show_array(arr_out, "arr_out in main()");

    // call 1st function
    printf("switch1() uses int[] as output\n");
    switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
    show_array(arr_out, "arr_out using 6..1 array as input and 1st function");

    // for 2nd function we need an array of pointers
    int* pArr[6] = {0};  // 6 pointers to int
    for (int i = 0; i < 6; i  ) pArr[i] = &arr_out[i];
    printf("switch2() uses int*[] as output\n");
    switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
    show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");

    return 0;
}

void switch1(const int in[], int out[], const int laenge)
{
    for (int i = 0; i < laenge; i  )
        out[i] = in[laenge - 1 - i];
}

void switch2(const int in[], int* out[], const int laenge)
{
    for (int i = 0; i < laenge; i  )
        *out[i] = in[laenge - 1 - i];
}

void show_array(const int array[6], const char* msg)
{
    printf("%s:\t", msg);
    for (int i = 0; i < 6; i  ) printf("%d ", array[i]);
    printf("\n");
}

output of the modified code

arr_out in main():      0 0 0 0 0 0
switch1() uses int[] as output
arr_out using 6..1 array as input and 1st function:     1 2 3 4 5 6
switch2() uses int*[] as output
arr_out using 1..6 array as input and 2nd function:     6 5 4 3 2 1

about the changes

void show_array(const int array[6], const char* msg)
{
    printf("%s:\t", msg);
    for (int i = 0; i < 6; i  ) printf("%d ", array[i]);
    printf("\n");
}

This function is a helper to show the array contents and accepts a title. Very convenient here

  • the 2 functions has no output (printf() calls)
  • parameters are declared const so we can build the vector at the function call
  • I am using shorter names and changed the order of arguments to input and then output
void switch1(const int in[], int out[], const int laenge)
{
    for (int i = 0; i < laenge; i  )
        out[i] = in[laenge - 1 - i];
}

void switch2(const int in[], int* out[], const int laenge)
{
    for (int i = 0; i < laenge; i  )
        *out[i] = in[laenge - 1 - i];
}

Here you see the difference between the 2 functions: a single asterisk.
But in order of using the second function you need to build the vector of pointers as here

    // call 1st function
    printf("switch1() uses int[] as output\n");
    switch1((int[6]){6, 5, 4, 3, 2, 1}, arr_out, 6);
    show_array(arr_out, "arr_out using 6..1 array as input and 1st function");

    // for 2nd function we need an array of pointers
    int* pArr[6] = {0};  // 6 pointers to int
    for (int i = 0; i < 6; i  ) pArr[i] = &arr_out[i];
    printf("switch2() uses int*[] as output\n");
    switch2((int[6]){1, 2, 3, 4, 5, 6}, pArr, 6);
    show_array(arr_out, "arr_out using 1..6 array as input and 2nd function");
  • Related