I am trying to insert an integer if array position is equal to 3 but unfortunately when I am printing my array a I am getting absured results like this:
0
0
2
whereas, the result expected is:
0
0
128
0
0
My code is:
#include<stdio.h>
int substitute_and_sum(int *a, int len)
{
int sum = 0, i;
for (i = 0; i < 5; i++)
{
if (i == 3)
{
a[i] = 128;
}
}
printf("%d\n", *a);
return 0;
}
int main()
{
int arr[] = { 0, 0, 2, 0, 0 };
int j = 5, i;
for (i = 0; i < 5; i++)
{
substitute_and_sum(&arr[i], j);
}
}
substitute_and_sumgoes outside the bounds of the array since you are giving it the array at various indices rather than the start of the array.