How to edit an array in another function than the main function in c programming?

I'd like to increase each element in the array by one in another function than the main function. Then, I'd like to call this function and print in the main function.

#include <stdio.h>

int function(int array2[5]) {

    int i;
    while(i<4) {
        array2[i]=array2[i]+1;
        i++;
    }

    return array2[5];
}

int main() {
int array[5]={1,2,3,4,5};
int answer;

answer[5]=function(array[5]);

int j;
while(j<4) {
    printf("%d \n",answer[j]);
    j++;
}

return 0;
}

3 answers

  • answered 2021-04-20 19:22 Garr Godfrey

    Some important things to know:

    • When you pass an array in C, you don't make a copy. It is the same array, so modifying the array that is passed in modifies the original.
    • The [] are operators and not part of the variable name.
    • The [] work differently in declaring a type than when used in an expression. array[5] gives you the 6th element in array, but int array[5] declares an array with 5 elements.
    • Accessing an element beyond the end of the allocated array has undefined behavior, but usually will crash.
    • If you declare a variable int answer it is not an array, and cannot become an array. It is just one int
    • If you want to make a copy of an array, you need to explicitly copy. There are standard libraries that might do it, but you should learn to copy each element, one by one, to the new array.

  • answered 2021-04-20 19:38 Kozmotronik

    There you go. I suppose this is what you want:

    #include <stdio.h>
    
    // Since the array named parameters are scoped only within its function,
    // they are apart from your array in the main.
    void function(int array[], int len) {
        
        int i = 0;
        while(i<len) {
            array[i]=array[i]+1;
            i++;
        }
    }
    
    // Or alternatively you can process the array using a pointer
    void functionWithPointer(int *array, int len) {
        
        int i = 0;
        while(i<len) {
            *(array+i) = *(array+i)+1;
            i++;
        }
    }
    
    int main() {
        int array[]={1,2,3,4,5};
    //  int answer; // Not necessary
        int length = sizeof(array) / sizeof(int); // !!!ATTENTION
        
        function(array, length);
        
        // The array values updated by 1
        printf("Array values after 1st update\n");
        for(int k=0; k<length; k++) {
            printf("%d \n",array[k]);
        }
        
        functionWithPointer(array, length);
        
        // The array values updated by 1 again
        printf("Array values after 2nd update\n");
        int j;
        while(j<length) {
            printf("%d \n",array[j]);
            j++;
        }
        
        return 0;
    }
    

    Here is the output:

    Array values after 1st update
    2 
    3 
    4 
    5 
    6 
    Array values after 2nd update
    3 
    4 
    5 
    6 
    7 
    

  • answered 2021-04-20 19:52 Vlad from Moscow

    The return type int of the function does not make a sense.

    int function(int array2[5]) {
    

    And moreover you are trying to return the non-existent 6-th element of an array with only 5 elements.

    return array2[5];
    

    Within the function you are using uninitialized variable i

    int function(int array2[5]) {
    
        int i;
        while(i<4) {
        //...
    

    that results in undefined behavior. Also the used magic number 4 does not make a sense at least because you are trying to pass to the function an array with 5 elements.

    The function should be declared with a second parameter that specifiers the number of elements in the passed array. This function declaration

    int function(int array2[5]);
    

    does not mean that the passed to the function array has exactly 5 elements. The compiler will adjust the parameter declaration of the function to pointer to the array element type like

    int function(int *array2);
    

    In this statement in main

    int answer;
    
    answer[5]=function(array[5]);
    

    you are using the subscript operator with an object of the scalar type int. So the compiler shall issue an error message.

    Here is a demonstrative program that shows how the function can be defined.

    #include <stdio.h>
    
    void function( int a[], size_t n ) 
    {
        for ( ; n--; ++a )
        {
            ++*a;
        }
    }
    
    int main(void) 
    {
        int a[] = { 1, 2, 3, 4, 5 };
        const size_t N = sizeof( a ) / sizeof( *a );
        
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", a[i] );
        }
        
        putchar( '\n' );
        
        function( a, N );
        
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%d ", a[i] );
        }
        
        putchar( '\n' );
        
        return 0;
    }
    

    The program output is

    1 2 3 4 5 
    2 3 4 5 6