I'm new to pointers and I'm trying to implement a simple code, in which i read and write an array and I also search for the minimum element between two given indexes. For some reason the write() function prints large values (I'm guessing they are the locations in which values are stored). Also I receive a SIGSECV when I run the poz_minVal function. I know this is a nooby question, but any help will de much appreciated!
The code is as follows:
#include<stdio.h>
#include<stdlib.h>
int read(int *v, FILE *f){
int i,length;
fscanf(f,"%d",&length);
v = (int *)malloc(length*sizeof(int));
for(i=0;i<length;++i)
fscanf(f,"%d",v+i);
return length;
}
void write(int *v, int length, FILE *g){
int i;
for(i=0;i<length;++i)
fprintf(g,"%d ",*(v+i));
fprintf(g,"\n\n");
}
int poz_valMin(int *v, int length,int left,int right){
int k,mini = *v; //set mini as the 1st element in v
for(k=left;k<right;++k)
if(*(v+k) < mini)
mini = *(v+k);
return mini;
}
int main(){
FILE *f = fopen("datein.txt","r");
FILE *g = fopen("dateout.txt","w");
int *v, n = read(v,f);
write(v,n,g);
fprintf("The minimum value between the indexes 2 and 4 is: %d.\n\n",poz_valMin(v,n,2,4));
return 0;
}