C 传递两个参数

C 传递两个参数,c,pointers,prototype,pass-by-reference,C,Pointers,Prototype,Pass By Reference,我想把两个参数传递到void中,从void赋值和void最大值除以void。我一次只学会通过一次辩论。你能告诉我我要做什么吗?把下面的变量打印出来。如果可能的话,我不希望我的代码格式发生剧烈的变化。你能给我举个例子吗,因为我是一个视觉学习者。谢谢 #include <stdlib.h> #include <stdio.h> #define Max 6 struct Numbers { double a,b,c,d,e,f; }; void Maximum(d

我想把两个参数传递到void中,从void赋值和void最大值除以void。我一次只学会通过一次辩论。你能告诉我我要做什么吗?把下面的变量打印出来。如果可能的话,我不希望我的代码格式发生剧烈的变化。你能给我举个例子吗,因为我是一个视觉学习者。谢谢

#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;
}
#包括
#包括
#定义最大值6
结构数
{
双a,b,c,d,e,f;
};
最大空隙率(双倍*ptr);
无效分割(双倍最大值,双倍ptr);
无效分配_编号()
{
结构编号;
a=45.78;
b=81.45;
c=56.69;
d=34.58;
数字e=23.57;
数字f=78.35;
最大值((双*)和数量);
除法((双*)和数字);
}
最大无效值(双倍*ptr)
{
int i=0;
双倍最大值=ptr[0];
(i;i最大值)
{
最大值=ptr[i];
}
}
除以(最大值);
}
无效分割(最大值加倍,ptr加倍)
{
printf(“%.2f”,最大值);
printf(“%.2f”,ptr[3]);
}
int main()
{
分配_编号();
返回0;
}
这里使用数组而不是struct-shwon和参考示例 就像约阿希姆·皮勒伯格说的。不要将结构用作数组。在您的例子中,使用多维数组

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;
}
不会像你想象的那样工作。“double Maximum”是一个新的双变量,在除法范围内工作,不是从函数中检索的变量:

 void Maximum(double *ptr);
如果您已经知道了这一点,那么您应该知道或者至少已经预料到变量的命名有多么糟糕(请将其放低)

现在让我们开始你想做的事情。除非我注意到什么,否则你的代码完全被破坏了。在Assign_numbers()中,要使用指针引用调用divising()。在max()中,您希望再次调用divising(),但这次只发送一个值。如果您有两个单独的不同调用,每个调用都有一个参数,这并不会让它变得更好。但是函数必须有两个参数。现在,为了遍历结构中的变量,同样不建议这样做,下面的代码只是一个示例

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;
}
结构编号
{
双a,b,c,d,e,f;
};
结构编号分配_编号()
{
结构编号;
a=45.78;
b=81.45;
c=56.69;
d=34.58;
数字e=23.57;
数字f=78.35;
返回号码;
}
int main()
{
结构编号;
编号=分配编号(编号);
double*value=&(number.a);//获取第一个元素的地址,因为指针总是向上计数。
int i;
/*这将循环遍历结构的地址,从数字.a中的初始地址开始,向上移动5次,希望以数字.f结束。构造数组的方法非常糟糕*/
/*只要试着用sizeof(double)替换sizeof(number),你会突然得到各种奇怪的值,因为你已经离开了结构*/
/*还要注意,这仅在结构中的所有数据类型的大小为8字节(大小为double)时才起作用*/
对于(i=0;i
新工作守则 说了这么多。这是我能够使您的代码工作的最接近的一次,因为我不知道您试图实现什么:

#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;
}
#包括
#包括
#定义最大值6
结构数
{
双a,b,c,d,e,f;
};
最大空隙率(双倍*ptr);
空隙分割(双*ptr);
无效分配_编号()
{
结构编号;
a=45.78;
b=81.45;
c=56.69;
d=34.58;
数字e=23.57;
数字f=78.35;
最大值(&number.a);//您需要分析结构的第一个地址。在本例中为“a”
除法(&number.a);
}
最大无效值(双倍*ptr)
{
int i=0;
双倍最大值=ptr[0];
(i;i最大值)
{
最大值=ptr[i];
}
}
printf(“最大值:%f”,最大值);
}
/*//删除了第一个参数,因为它不清楚它的用途,并且您只使用一个参数对此函数进行了函数调用*/
空隙分割(双*ptr)
{
printf(“%.2f”,ptr[3]);
}
int main()
{
分配_编号();
返回0;
}

你看到你打的那些
printf
电话了吗?你可以用两个参数来创建它们。哦,就像我之前说的,你不能可靠地使用结构作为数组。虽然在这种特殊情况下,它可能在您的编译器和标准库的操作系统上工作,但不要这样做!它是不可移植的,它会在你最不经意的时候导致奇怪的行为,并使代码无法维护。如果我是你的教授,而你给我这样的代码,我会立即让你不及格。@JoachimPileborg不仅正确地认为这个代码是不可移植的(它依赖于未定义的行为),而且荒谬地缺乏灵活性。当同样(甚至更少)的工作量可以给你一个可以计算任意数字数组的最大值的函数时,任何人编写一个可以计算最多6个数字的函数有什么可能的动机呢?Joachim Pileborg或John Coleman-我是C新手,所以我只是在试验。你能给我解释一下为什么它不能随身携带吗?为什么在函数本身中初始化数组更好?我试图找到关于它的文件,但我的书并没有详细解释。谢谢。您假设您知道结构的内存布局——但是C标准没有指定它。特别是,有时插入的struct元素之间会有填充,以便更好地将字段与处理器的字大小对齐。而且——在学习C语言时,至少在一开始,多阅读少实验可能是一个更好的策略。C语言不是一种可以通过反复试验来学习的语言。
#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;
}