#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 程式員來說這可能看起來很愚蠢,但我們將不勝感激。我想將輸入輸入到結構陣列中,然后在已通過參考呼叫的函式中使用它。 請告訴我有人可以幫我說明我對邏輯的誤解嗎?
uj5u.com熱心網友回復:
功能
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("%f\n", 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 進行編譯。
uj5u.com熱心網友回復:
你的 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("%f\n", 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 = %f\n",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=%f\n",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=%f\n",j 1,storage[j].total);
grand_total = storage[j].total;
}
return grand_total;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/537271.html
標籤:C功能指针
上一篇:Node**list雙指標在使用鏈表的佇列的enqueue函式中的作用是什么?
下一篇:如何將指標轉換為多維切片/陣列
