#include <stdio.h>
int i, n;
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add;
int main()
{
printf("Enter full name : ");
scanf(" %[^\n]s", add.fullname);
printf("Enter the no. of stocks you want to purchased : ");
scanf("%d", &n);
for (i = 0; i < n; i )
{
printf("Enter the name of the company : ");
scanf(" %[^\n]s", add[i].com_name);
printf("Enter the no. of shares you want to purchased : ");
scanf("%d", &add[i].shares);
printf("Enter the price of each share : ");
scanf("%f", &add[i].price);
add.total = add.shares * add.price;
printf("Toatl money invested in this stock : ");
scanf("%f", &add[i].total);
}
printf("Total money invested : ");
scanf("%d", add.totalmoney);
return 0;
}
有問題的是 add_stock ,而不是 add stock ,我這樣寫是因為它不接受問題。所以,我得到“添加”的錯誤,說下標值既不是陣列也不是指標也不是向量。
uj5u.com熱心網友回復:
要宣告一個結構陣列,您需要定義如下變數,
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add[20];
這使得 add 作為 add_stock 元素的陣列。然后你可以訪問它,add[0].total、add[1].price 等等。
如果要宣告需要具有動態元素數量的陣列,可以按如下方式進行。
struct add_stock* arStock;
//allocate memory to hold 10 elements
arStock = (add_stock*)malloc( 10 * sizeof(struct add_stock));
arStock[0]->total=10; //access it with ->, instead of .
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/467417.html
標籤:C
上一篇:makefile不改變輸入檔案名
