传递两个论点

时间:2016-03-13 13:14:32

标签: c pointers prototype pass-by-reference

我想将两个参数传递给 void Dividing void Assign_numbers void Maximum 。我只是学会了一次传递一个论点。你能告诉我我要做什么打印 void Dividing 中的以下变量。如果可行,我不希望我的代码格式发生巨大变化。你能告诉我一个例子,因为我是一个视觉学习者。感谢

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

#define Max 6

struct Numbers
{
    double a,b,c,d,e,f;
};

void Maximum(double *ptr);
void Dividing(double Maximum, double *ptr);

void Assign_numbers()
{
    struct Numbers number;

    number.a=45.78;
    number.b=81.45;
    number.c=56.69;
    number.d=34.58;
    number.e=23.57;
    number.f=78.35;

    Maximum((double*) &number);
    Dividing((double*) &number);
}

void Maximum(double *ptr)
{
    int i=0;
    double Maximum = ptr[0];

    for(i;i<Max;i++)
    {
        if(ptr[i]> Maximum)
        {
            Maximum = ptr[i];
        }
    }
    Dividing(Maximum);
}

void Dividing(double Maximum, double *ptr)
{
    printf("%.2f", Maximum);
    printf("%.2f",ptr[3]);
}

int main()
{
    Assign_numbers();
    return 0;
}

1 个答案:

答案 0 :(得分:1)

使用数组代替struct - shwon,参考示例

像Joachim Pileborg所说。不要将结构用作数组。在您的情况下使用多维数组。

double[10][6] numbers;

您可以轻松地迭代这样的数组:

#include <stdio.h>

int main () {

   /* an array with 2 rows and 6 columns*/
   double numbers[2][6] = { 
       {45.78, 81.45, 56.69, 34.58, 23.57, 78.35},
       {1,2,3,4,5, 6}
   };

   int i, j;
   /* output each array element's value */
   for ( i = 0; i < 2; i++ ) {
      for ( j = 0; j < 6; j++ ) {
         printf("numbers[%d][%d] = %f\n", i,j, numbers[i][j] );
      }
   }
   /* Output by reference */
   for(i = 0; i < 2; i++){
       for(j=0; j < 6; j++ ){
           printf("numbers[%d][%d] = %f\n", i, j,*(*(numbers + i) + j));
       }
   }
   return 0;
}

为什么当前代码失败

现在解释你的代码(不)如何工作以及指针如何工作。首先:

Dividing(double Maximum, double* ptr);

不按您认为的方式工作。 &#34; double Maximum&#34;是一个新的双变量,它在Divide的范围内工作,而不是从函数中检索的变量:

 void Maximum(double *ptr);

如果您已经知道这一点,那么您应该知道或至少预计变量的命名有多差(保持lowerCamelCase)。

现在让我们开始你想要做的事情。恕我直言,除非我注意到某些事情,否则你的代码完全被破坏在Assign_numbers()中,您希望使用指针引用调用Dividing()。在Maximum()中,您想再次调用Dividing(),但这次只发送一个值。如果您有两个不同的调用,每个调用都有一个参数,那么它并不会更好。但该功能必须有两个参数。现在,为了遍历结构中的变量 - 不建议这样做,而底部代码仅作为示例。

struct Numbers
{
    double a,b,c,d,e,f;
};

struct Numbers Assign_numbers()
{
    struct Numbers number;
    number.a=45.78;
    number.b=81.45;
    number.c=56.69;
    number.d=34.58;
    number.e=23.57;
    number.f=78.35;

    return number;
}

int main()
{
    struct Numbers number;
    number = Assign_numbers(number);

    double *value = &(number.a); //take address of the first element, since a pointer always counts upwards.
    int i;
    /*This loops through the addresses of the struct starting from the initial address in number.a and moves upwards 5 times and hopefully ends in number.f. Seriously bad way to construct arrays*/
    /*Just try replacing sizeof(number) with sizeof(double). suddenly you get all kinds of weird values because you have ended up outside of the struct*/
    /*Also note that this only works when all the datatypes in the struct have a size of 8 bytes(the size of double) */
    for (i = 0; i < sizeof(number) / sizeof(double); i++){
        printf("[%d]: %f\n",i, value[i]);
    }

    return 0;
}

新工作代码

尽管如此。这是我能够使你的代码工作的最接近的,因为我不知道你想要完成什么:

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

#define Max 6

struct Numbers
{
    double a,b,c,d,e,f;
};

void Maximum(double *ptr);
void Dividing(double *ptr);

void Assign_numbers()
{
    struct Numbers number;

    number.a=45.78;
    number.b=81.45;
    number.c=56.69;
    number.d=34.58;
    number.e=23.57;
    number.f=78.35;

    Maximum(&number.a); //You need to parse the very first address of the struct. IN this case 'a'
    Dividing(&number.a);
}

void Maximum(double *ptr)
{
    int i=0;
    double maximum = ptr[0];

    for(i;i<Max;i++)
    {
        if(ptr[i]> maximum)
        {
            maximum = ptr[i];
        }
    }
    printf("maximum: %f", maximum);
}

/*//removed the first parameter since it was not clear what it was for and you only had function calls to this function with one parameter */
void Dividing(double *ptr)
{
    printf("%.2f",ptr[3]);
}

int main()
{
    Assign_numbers();
    return 0;
}
相关问题