【问题标题】:Passing an array of strcture to a function in C将结构数组传递给C中的函数
【发布时间】:2021-02-15 22:50:20
【问题描述】:

我是 C 语言的新手。目前,我正在学习 C 数据类型和函数。

我正在尝试将结构数组传递给另一个函数。 我的代码如下

 typedef struct {
   int indexNumber;
   float chemResult;
   float mathsResult;
   float phyResult;
 }stuResult;

 const int STU_NUM =10;

 void addStudent(stuResult stuData[]){
   ..........
 }
 
 int main(){
   stuResult stuData[STU_NUM];
   
   addStudent(stuData);
 }

这给了我一个错误“finction main 中的未知类型名称 stuResult” 我无法弄清楚我做错了什么。请帮帮我。

我正在使用 GNU GCC 编译器

【问题讨论】:

  • 提示:尽量避免使用全局变量。确保在 addStudent 等函数中接受数组/大小对,而不是假设全局 const 适用。
  • 如果我用任何合理的东西替换.....,你的代码编译得很好。您可能有一个错字,您没有包含在您发布的代码中。这就是为什么我们要求您包含一个stackoverflow.com/help/minimal-reproducible-example,您应该确保重新创建问题(通常这样做的练习会显示答案),然后直接将其剪切并粘贴到您的问题中(而不是重新输入它)

标签: arrays c function data-structures


【解决方案1】:

typedef struct { ... } y 创建一个名为y变量typedef struct y { ... }struct y 创建别名 y

更正的符号:

typedef struct stuResult {
  int indexNumber;
  float chemResult;
  float mathsResult;
  float phyResult;
};

【讨论】:

    猜你喜欢
    • 2011-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多