-3

我需要找到最大的数字(双数)。我的程序有什么问题?它没有编译。

#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 is %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;
    }
}
4

3 回答 3

1

这里有一个问题:

double *parr=&arr[5];

arr只有5个元素。所以有效的索引范围是 0 - 4。

我在代码中评论了这些问题:

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

void dobig(double *parr,int *len,double *big); // Prototype didn't match. You want to 
       // pass a pointer, not an array of pointers.

    int main(int argc, char *argv[]) // Standard way to write main()
    {
    double big;
    double arr[5]={1.00,2.321,6.54337,4.333333,5.5555555};

    double *parr=&arr[0]; // If you want to loop through the array 
           //in dobig(), you have to pass the pointer to the first element or 
           //char   *parr=arr; is also fine.

    int size=sizeof(arr)/sizeof(double); // You want to know how many double's 
                  // are there in the array. So use sizeof(double)
    int *len=&size;
    dobig(parr,len,&big);
    printf("the largest number is %f\n",big); // To print a double, 
                                   //use %f. %p is for pointer.
    }

    void dobig(double *parr,int *len,double *big)
    {
    int i;
    double pbig=*parr;
    for(i=0;i<*len;i++)  // Here it doesn't enter the
                // loop at all due to incorrect condition.
    {
        if(pbig<*(parr+i))
        {
           pbig=*(parr+i);
        }
    }
    *big=pbig;
    }
于 2013-04-20T07:12:19.833 回答
0

改变

void dobig(double *parr[5],int *len,double *big);

进入

void dobig(double *parr, int *len, double *big);

注意:我建议不要使用void main但是int main

于 2013-04-20T07:15:00.703 回答
-1

U 正在传递一个数组,因此您需要在 dobig 函数中使用双指针

这是您的代码,并进行了必要的更正。

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

void dobig(double *parr[5],int *len,double *big);

int  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 is %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;
}
}
于 2013-04-20T07:23:51.647 回答