传递两个参数
Passing two arguments
我想将两个参数从 void Assign_numbers 和 void Maximumvoid Dividing 传递给 void Dividing =17=]。我只学会了一次通过一个论点。你能告诉我我必须做什么打印出 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;
}
使用数组而不是结构 - 此处显示参考示例
正如 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);
并不像您认为的那样工作。 "double Maximum" 是一个新的双精度变量,它在除法范围内工作,而不是从函数中检索到的变量:
void Maximum(double *ptr);
如果您已经知道这一点,那么您应该知道或至少已经预料到您的变量命名有多糟糕(保持小驼峰命名)。
现在让我们开始了解您正在尝试做的事情。恕我直言,除非我注意到什么,否则您的代码已完全损坏。在 Assign_numbers() 中,您想使用指针引用调用 Dividing()。在 Maximum() 中,您想再次调用 Dividing(),但这次只发送一个值。您有 2 个单独的不同调用,每个调用都有一个参数,这并没有使它变得更好。但是函数必须有两个参数。现在为了遍历结构中的变量——同样不推荐这样做,底部代码仅作为示例。
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;
}
我想将两个参数从 void Assign_numbers 和 void Maximumvoid Dividing 传递给 void Dividing =17=]。我只学会了一次通过一个论点。你能告诉我我必须做什么打印出 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;
}
使用数组而不是结构 - 此处显示参考示例
正如 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);
并不像您认为的那样工作。 "double Maximum" 是一个新的双精度变量,它在除法范围内工作,而不是从函数中检索到的变量:
void Maximum(double *ptr);
如果您已经知道这一点,那么您应该知道或至少已经预料到您的变量命名有多糟糕(保持小驼峰命名)。
现在让我们开始了解您正在尝试做的事情。恕我直言,除非我注意到什么,否则您的代码已完全损坏。在 Assign_numbers() 中,您想使用指针引用调用 Dividing()。在 Maximum() 中,您想再次调用 Dividing(),但这次只发送一个值。您有 2 个单独的不同调用,每个调用都有一个参数,这并没有使它变得更好。但是函数必须有两个参数。现在为了遍历结构中的变量——同样不推荐这样做,底部代码仅作为示例。
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;
}