I have a code written in c, basically it takes in an array and prints it backward. A pretty basic thing. Here's the code:
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
int main(){
int n;
scanf("%d",&n);
int *arr = malloc(sizeof(int) * n);
for(int arr_i = 0; arr_i < n; arr_i++)
{
scanf("%d",&arr[arr_i]);
}
for(arr_i=n-1; arr_i >= 0;arr_i--)
{
printf("%d ",arr[arr_i]);
}
return 0;
}
I get the following error for the second for loop:
solution.c:17:9: error: ‘arr_i’ undeclared (first use in this function)
for(arr_i=n-1; arr_i >= 0;arr_i--)
When I insert int
before the arr_i
in the second for
loop the error goes away.
So, my doubt is that why is that even though I have already declared arr_i
in first for
loop, it asks me to declare it once again in the second for loop?