C++ 读入函数内部的结构数组?

标签 c++ arrays function struct

我试图将一个 int 读入一个结构数组,但在尝试编译时我在“[”之前收到预期表达式的错误。

struct department {
    int id;
    char name[20];
};

int addnewdep(struct department[],int d);
int main()
{
.....
}
int addnewdep(struct department[],int d)
{
    cin >> department[d].id;
    cin >> department[d].name;
}

错误出现在函数定义中。 我不确定如何修复此错误。任何帮助都会很好,谢谢。

最佳答案

应该是:

  int addnewdep(department dep[], int d) {
     cin >> dep[d].id;
     cin >> dep[d].name;
  }

因为 department 是类型的名称,而不是函数的参数。另请注意额外的;在您的代码中。

不需要在定义之前声明 addnewdep()。

关于C++ 读入函数内部的结构数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491579/

相关文章:

c++ - qnetwork没有回复状态码或错误,但失败

c++ - float 数组对齐错误

arrays - 如何在 UI 中显示解析后的 JSON 数据?

c++ - 解析有符号和无符号 int 之间的 char 是否未指定?

flash - AS3中匿名函数有什么用?

c++ - 什么是 "cerr"和 "stderr"?

c++ - 为什么在使用 operator class_name() & c 样式转换时 g++ 会出现错误

arrays - 如何在 Ruby 中拆分重复元素的数组

php - 返回数组中深度未知的变量

c - 将变量设置为函数的返回类型