Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass pointer of arrays to functions

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading