c++ - Returning pointers from function gives weird numbers -
iam trying max number of array has numbers -20 30 returns weird numbers --> 2255667 impossible if going well.
int * ptomx(int a[],int n) { int max=-100; int *point; for(int i=0;i<=n;i++) { if(max<a[i]) { max = a[i]; } } point = new int; *point = max; return point; } this in main()
int maxtemp; maxtemp=*(ptomx(a,n)); cout<<"max temp is:"<<maxtemp; is because pointer in function destroyed after function returns , gives random number in memory location. if thats case needs dereferenced how?
you reading values beyond end of array. these values can anything , what's causing incorrect results.
for(int i=0;i<=n;i++) //^^ should changed to
for(int i=0;i < n;i++)
Comments
Post a Comment