我很难将指针结构作为 C 中的函数参数。我是初学者,我很困惑


#include<stdio.h>
struct create
{
int a;
int b;
int c;
float total;
};
struct create Create[3];
float givename(struct create* storage[3]);
int main()
{
for(int j = 0; j<4; ++j)
{
printf("Input a[%d]:", j+1);
scanf("%d", &Create[j].a);
printf("Input b[%d]:", j+1);
scanf("%d", &Create[j].b);
printf("Input c[%d]:", j+1);
scanf("%d", &Create[j].c);
float sum = givename(&Create);
printf("%f", sum);
}
}
float givename(struct create* storage[3])
{
for(int i = 0; i<4; ++i)
{
storage[i]->total = storage[i]->a + storage[i]->b + storage[i]->c;
return storage[i]->total;
}
}

这是我写的东西,当然它不起作用,对于精英C程序员来说,这似乎很愚蠢,但帮助将不胜感激。我想将输入到结构数组中,然后在通过引用调用的函数中使用它。请告诉我有人可以帮我展示我对逻辑的误解吗?

函数

float givename(struct create* storage[3])

获取指向struct create的指针数组,您正在发送struct create数组的地址。如果你想要一个指向struct create的指针数组,请像这样做:

struct create * array[3];
array[0] = & Create[0];
array[1] = & Create[1];
array[2] = & Create[2];

另外,由于返回,此函数在第一次迭代期间停止。

数组的大小是 3,你循环了 4 次。

您的main需要一个返回值;

如果你想将整个数组传递给givename(一个struct create数组,而不是指针),你不需要,因为Create是全局的;

对于第一个循环中的每次迭代(修复后 3 次),您迭代整个数组,我怀疑这是您想要做的。

以下版本提示填充 3 个struct create,计算每个的总数(并存储它),然后打印它。 这是你想做的吗?

#include<stdio.h>
#include <stdlib.h>
struct create
{
int a;
int b;
int c;
float total;
};
struct create Create[3];
float givename(struct create * storage);
int main()
{
for(int j = 0; j < 3; ++j)
{
printf("Input a[%d]:", j+1);
scanf("%d", & Create[j].a);
printf("Input b[%d]:", j+1);
scanf("%d", & Create[j].b);
printf("Input c[%d]:", j+1);
scanf("%d", & Create[j].c);
float sum = givename(& Create[j]);
printf("%fn", sum);
}
return EXIT_SUCCESS;
}
float givename(struct create * storage)
{
storage->total = storage->a + storage->b + storage->c;
return storage->total;
}
echo 1 2 3 4 5 6 7 8 9 | yourProgram
>> Input a[1]:Input b[1]:Input c[1]:6.000000
>> Input a[2]:Input b[2]:Input c[2]:15.000000
>> Input a[3]:Input b[3]:Input c[3]:24.000000

你想实现什么还不清楚,命名也无济于事,我建议你给更好的名字:

  • struct create没有描述里面存储的内容

考虑使用 -Wall -Wextra -Werror 进行编译。

你的 givename()(似乎是一个坏名字)目前只处理一个结构,因为它在循环内返回。在每次输入 3 个整数后调用它的方式,它只会处理一个结构(每次第一个结构),而不是结构数组。要处理结构数组,您可以在获得所有输入后调用它 - 在循环外部获取输入。这显示了每个结构在循环期间的版本处理,然后在循环之后对所有输入进行版本处理。

#include<stdio.h>
struct create
{
int a;
int b;
int c;
float total;
};
struct create Create[3];
float givename(struct create *storage);
float calculate_totals(struct create storage[3]);
float get_grand_total(struct create [3]);
int main()
{
for(int j = 0; j<3; ++j)
{
printf("Input a[%d]:", j+1);
scanf("%d", &Create[j].a);
printf("Input b[%d]:", j+1);
scanf("%d", &Create[j].b);
printf("Input c[%d]:", j+1);
scanf("%d", &Create[j].c);
float sum = givename(&Create[j]);
printf("%fn", sum);
}
get_grand_total(Create);

/* calculate all totals after input */ 
for(int j = 0; j<3; ++j)
{
printf("Input a[%d]:", j+1);
scanf("%d", &Create[j].a);
printf("Input b[%d]:", j+1);
scanf("%d", &Create[j].b);
printf("Input c[%d]:", j+1);
scanf("%d", &Create[j].c);
}     
float grand_total = calculate_totals(Create);
printf("grand total = %fn",grand_total);    
}
float givename(struct create *storage)
{
storage->total = storage->a + storage->b + storage->c;
return storage->total;
}
float get_grand_total(struct create storage[3])
{
float grand_total = 0;
for (int i = 0; i < 3; i++)
{
grand_total += storage[i].total;   
}
printf("grand total sum=%fn",grand_total);
return grand_total;
}
float calculate_totals(struct create storage[3])
{
float grand_total = 0;
for(int j = 0; j<3; ++j)
{
storage[j].total = storage[j].a + storage[j].b + storage[j].c;
printf("total for struct %d=%fn",j+1,storage[j].total);
grand_total += storage[j].total;
}
return grand_total;
}

相关内容