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