Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

closed How can I update value of an arguments of an array using functions ?

+6 votes
asked Feb 7, 2022 by Shahriyor Yuldashev (690 points)
closed Feb 21, 2022 by Admin

For example, I have an array that has 10 values. 

my_array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

And I want to update the value of the number that is in the 5-th index.

void update_value(int a){

    a = 10;

}

How can I do this ?

closed with the note: answered

2 Answers

+1 vote
answered Feb 8, 2022 by Peter Minarik (86,160 points)

If you're in the same scope as your array, you can just simply index it and assign it a value.

If you want to do it from a different function, normally the arguments are copies. In your example, a is just a copy of whatever was passed to the function. To be able to change your array element from a function, you need to pass in a pointer (memory address) to the element you want to change, then change the value stored in the given pointer (memory address) by dereferencing it first.

One more alternative is to pass in the whole array to your function and the index where you want to change the element. Arrays are really just memory addresses, that's why you can change them if you pass them to another function.

I hope the below example helps:

#include <stdio.h>

void print(const int * array, size_t size)
{
    for (size_t i = 0; i < size; i++)
    {
        if (i > 0)
            printf(", ");
        printf("%d", array[i]);
    }
    printf("\n");
}

void change(int * pElement, int newValue)
{
    *pElement = newValue;
}

#define SIZE 10u

int main()
{
    int my_array[SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    print(my_array, SIZE);

    // changing the element in the array takes only an index
    my_array[3] = 33;

    // Changing an element in a function requires to pass the address to that element, not the value stored in the element.
    change(&my_array[4], 44);
    
    print(my_array, SIZE);
    return 0;
}
commented Feb 8, 2022 by Shahriyor Yuldashev (690 points)
Thank you for your answer!
commented Feb 9, 2022 by Peter Minarik (86,160 points)
My pleasure.
0 votes
answered Feb 11, 2022 by Anusha (140 points)

you can do this by 2 types, one is you can declare array as globally or else locally

If you want to make it globally you can use this code

/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <stdio.h>
#define LENGTH 10
void change_value(int new_value, int index);
int array[10] = {1,2,3,4,5,6,7,8,9,10};

int main()
{
    change_value(44, 4);
}

void change_value(int new_value, int index)
{
    int i,j;
    for(i = 0; i < LENGTH;i++)
    {
        if(i == index)
        {
            array[i] = new_value;
            break;
        }
    }
    for(j = 0; j<LENGTH;j++)
    {
        printf("%d\t",array[j]);
    }
    printf("\n");

}

Array as Local variable

here you should use pointer function to collect it

#include <stdio.h>
#define LENGTH 10
int  *change_value(int *array_ptr,int new_value, int index);


int main()
{
    int array[10] = {1,2,3,4,5,6,7,8,9,10};
    int *ptr, j;
   
    ptr = change_value(array,44, 4);
    
    for(j = 0; j<LENGTH;j++)
    {
        printf("%d\t",ptr[j]);
    }
    printf("\n");
}

int *change_value(int *array_ptr,int new_value, int index)
{
    int i;
    for(i = 0; i < LENGTH;i++)
    {
        if(i == index)
        {
            array_ptr[i] = new_value;
            break;
        }
    }
    return array_ptr;
}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...