Declaration of vector with pointer

Hy guys!I have just began learning visual c++ and i have a little problem.I have the following code:


#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int citire_vector(int n,int *pv)
{int i;
pv=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++){printf("v[%d]=",i);
scanf("%d",pv);

}
free(pv);
pv=NULL;
return 0;}


void main()
{int n,v[10],i;
printf("Dimensiunea Vectorului este:");
scanf("%d",&n);
citire_vector(n,v);
for(i=0;i<n;i++)printf("v[%d]=%d\t",i,v[i]);
getch();
}


The problem is that when i try to write the vector i get this results:
v[0]=1
v[1]=2
v[2]=3
v[0]=-858993460 v[1]=-858993460 v[2]=-858993460

Why the result does not match my declaration?


sorry for the bad english.....i am in a hurry!!



Please use [code][/code] tags.

Anyway, the problem is that v[] in main is created and is filled with garbage. You call the function, and dynamically allocate some space, fill in some data, then free it (all unrelated to v[]). This has accomplished nothing, as when you return to main() none of that matters.

Why are you dynamically allocating at all? You already have an array in main(), just use that in the function.
I am dynamically allocating because i make this program as a homework for school!
Regarding the code tags.i am sorry!i will be more carefull in the future!
Thanks for your help!
Topic archived. No new replies allowed.