C Program Not Compiling -
i need find largest number (which double). problem in program? isn't compiling.
#include<stdio.h> #include<stdlib.h> void dobig(double *parr[5],int *len,double *big); void main() { double *big; double arr[5]={1.00,2.321,3.54337,4.333333,5.5555555}; double *parr=&arr[5]; int size=sizeof(arr)/sizeof(int); int *len=&size; dobig(parr,len,big); printf("the largest number %p/n",*big); system("pause"); } void dobig(double *parr,int *len,double *big) { int i; double pbig=*parr; for(i=0;i>*len;i++) { if(pbig<*(parr+i)) { pbig=*(parr+i); } *big=pbig; } }
here's problem:
double *parr=&arr[5];
arr
has 5 elements. valid index range 0 - 4.
i have commented inside code problems:
#include<stdio.h> #include<stdlib.h> void dobig(double *parr,int *len,double *big); // prototype didn't match. want // pass pointer, not array of pointers. int main(int argc, char *argv[]) // standard way write main() { double big; double arr[5]={1.00,2.321,6.54337,4.333333,5.5555555}; double *parr=&arr[0]; // if want loop through array //in dobig(), have pass pointer first element or //char *parr=arr; fine. int size=sizeof(arr)/sizeof(double); // want know how many double's // there in array. use sizeof(double) int *len=&size; dobig(parr,len,&big); printf("the largest number %f\n",big); // print double, //use %f. %p pointer. } void dobig(double *parr,int *len,double *big) { int i; double pbig=*parr; for(i=0;i<*len;i++) // here doesn't enter // loop @ due incorrect condition. { if(pbig<*(parr+i)) { pbig=*(parr+i); } } *big=pbig; }
Comments
Post a Comment