Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing array inside function in C

I am learning C and confused why a array created in the main wont change inside the function, i am assuming the array passed is a pointer, and changing the pointer should've change the array , right ? can someone explain what is happening in this case?

thx for the help.

int main(){
    int i, length = 10;
    int array[length];

    for (i = 0 ; i < length ; i++)
        array[i] = i * 10;
    printf("Before:");
    print(array, length);
    change(array, length);
    printf("After:");
    print(array, length);

    return 0;
}

// Print on console the array of int
void print(int *array,int length)
{
    int i;
    for(i = 0 ; i < length ; i++)
        printf("%d ", array[i]);
    printf("\n");
}

// Change the pointer of the array
void change(int *array,int length)
{
    int *new = (int *) malloc(length * sizeof(int));
    int i;
    for(i = 0 ; i < length ; i++)
        new[i] = 1;
    array = new;
}

I expected to see the following output:

Before:0 10 20 30 40 50 60 70 80 90 
After:1 1 1 1 1 1 1 1 1 1 

What i get:

Before:0 10 20 30 40 50 60 70 80 90 
After:0 10 20 30 40 50 60 70 80 90 
like image 256
Victor Ferreira Avatar asked Jan 17 '16 21:01

Victor Ferreira


People also ask

Can you change an array in a function in C?

To change an array in the function we have to pass an array in the function. For this, an array_name is passed to the function in the form of an actual argument.

How do you change an array within a function?

If you've entered a single-cell array formula, select the cell, press F2, make your changes, and then press Ctrl+Shift+Enter..

Can you modify an array?

Given an array, we need to modify values of this array in such a way that sum of absolute differences between two consecutive elements is maximized. If the value of an array element is X, then we can change it to either 1 or X.

How do you change values in an array?

To change the value of all elements in an array:Use the forEach() method to iterate over the array. The method takes a function that gets invoked with the array element, its index and the array itself. Use the index of the current iteration to change the corresponding array element.


2 Answers

In c you can't pass a variable by reference, the array variable that you assign inside the function contains initially the same address as the passed pointer, but it's a copy of it so modifying it will not alter the passed pointer.

You need to pass the address of the pointer in order to be able to alter it, like this

// Change the pointer of the array
void change(int **array, int length)
{
    *array = malloc(length * sizeof(int));
    if (*array == NULL)
        return;
    for (int i = 0 ; i < length ; i++)
        (*array)[i] = 1;
}

Then in main() you cannot assign to an array, doing so through this kind of function is surely undefined behavior. The array defined in main() is allocated on the stack and you cannot assign anything to an array since they are non-writeable lvalues so you cannot make it point to a heap memory location obtained with malloc(), you need to pass a pointer like this

int *array;
change(&array, length);
free(array);

If you want the function to replace the previous array, it will have to free() the malloc()ed data (note that passing NULL to free() is well defined), so

// Change the pointer of the array
void change(int **array, int length)
{
    free(*array);

    *array = malloc(length * sizeof(int));
    if (*array == NULL)
        return;
    for (int i = 0 ; i < length ; i++)
        (*array)[i] = 1;
}

then in main()

int *array;
array = NULL;
change(&array, length);
change(&array, length);
change(&array, length);
change(&array, length);
free(array);

will do what you apparently want.

like image 189
Iharob Al Asimi Avatar answered Sep 26 '22 21:09

Iharob Al Asimi


Ok, i will make the answer short.

  1. Arrays are always passed by reference in C

change(array, length); In this line, it means reference to the first element of the array variable is passed to the function.

Now the function needs a pointer to catch the reference, it can be a pointer or it can be an array. Note that the pointer or the array is local to the function.

  1. You received it in a pointer named array. Which is definitely a pointer but it is not the same as array in main function. It is local to the change function.

  2. You declared a new array dynamically, and assigned a pointer named new with it. And all the changes you did were to new pointer.

Everything is ok till now.

  1. Now you assigned the new pointer to the array pointer which is local to the change function.

This is the real issue. As even though the array is in heap section, and it will remain in the memory, but the *array and *new are local pointer variables.

Also array pointer in change function is different from array pointer in main function.

  1. The solution to this problem is Manipulate the data of array pointer directly.

    void change(int *array,int length) { int i; for(i = 0 ; i < length ; i++) array[i] = 1; }

In this way you are directly overwriting values of array in main function. Everything else is correct.

Summerization: array pointer in change function is local to change function. array in main function is local to main function. Making change function's local array pointer point to array in heap section won't change data in actual array. You changed pointer's position, Not the array's data.

like image 23
Kandy Avatar answered Sep 22 '22 21:09

Kandy