结构类型数组,C错误

时间:2013-03-18 18:05:38

标签: c arrays pointers struct

所以,我有这个结构:

typedef struct {
    int day;
    int amount;
    char type[4],desc[20];
 }transaction;

此函数用于填充在main中声明的事务类型的向量:

void transact(transaction t[],int &n)
 {
    for(int i=0;i<n;i++)
    {
        t[i].day=GetNumber("Give the day:");
        t[i].amount=GetNumber("Give the amount of money:");
        t[i].type=GetNumber("Give the transaction type:");
        t[i].desc=GetNumber("Give the descripition:");
    }
 }

我在函数transact()的标题处得到的错误:

  

Multiple markers at this line
  - Syntax error
  - expected ';', ',' or ')' before '&' token

2 个答案:

答案 0 :(得分:6)

您正在尝试将n参数声明为引用(int &n)。但引用是C ++特性,在C中不存在。

由于该函数没有修改n的值,只需将其设为正常参数:

void transact(transaction t[],int n)

您稍后在尝试分配数组时也会遇到错误:

t[i].type=GetNumber("Give the transaction type:");
t[i].desc=GetNumber("Give the descripition:");

由于GetNumber可能会返回一个数字,因此不清楚您要在那里做什么。

答案 1 :(得分:3)

C ++有int &n之类的引用; C没有。

删除&

您在向t[i].typet[i].desc分配号码时遇到问题。它们是字符串,你不能分配这样的字符串,你应该使用类似void GetString(const char *prompt, char *buffer, size_t buflen);的东西来进行读取和赋值:

GetString("Give the transaction type:", t[i].type, sizeof(t[i].type));
相关问题