i am a beginner in c. i don't know that much how to use arrays as function parameters, arguments or how to return array from an function. to my knowledge, the below code should work fine. But i can't get where the problem is. the function is not working as it should.
//reversing an array using function
#include<stdio.h>
void rev(int array[],int length)
{
int k,j,temp;
for(k=length-1,j=0;k>=0&&j<length;k--,j++){
temp=array[k];
array[k]=array[j];
array[j]=temp;
}
}
int main()
{
int c,arr[]={1,2,3,4,5,6,7,8,9};
rev(arr,9);
for(c=0;c<9;c++){
printf("%d ",arr[c]);
}
return 0;
}
int array[]
really meansint *array
, a pointer (this applies only to parameter declarations). Suggested reading: section 6 of the comp.lang.c FAQ. – Keith Thompson Aug 6 '12 at 9:39