c - 在 C 中传递结构数组的元素

标签 c function struct parameter-passing argument-passing

我试图传递我制作的 20 个“数据库”结构之一

这是我的函数“add”的原型(prototype)

void add(struct database test);

我想传递我的数据库结构,现在我只称它为“测试”

这是我的数据库结构

struct database
{
    char ID[6];
    char Duration[3];
};

main()
{

   char menu;
   struct database employee[20]; // I make 20 employee variables
   int a = 0;                    /*A counter I use to edit certain structs 
                                   ie a=2  "employee[a]" = "employee[2]" */

然后我这样调用这个函数:

add(employee[a]);
a++;  /*Once it exits this I want it to go to the next employee
       variable so I increment the counter */

实际的功能是这样的:

void add(struct database test)
{
    static int a = 0;

    printf("\nPlease enter a 5 digit employee number: ");
    scanf("%s", test[a].ID);

    a++
}

在执行此操作时出现错误:

错误 E2094 Assignment.c 64:函数 add(database) 中“int”类型参数的“operator+”未在类型“database”中实现

它说错误在

scanf("%s", test[a].ID);

在此先感谢您的帮助,如果我的格式有误,我深表歉意,仍在学习如何使用堆栈溢出,非常抱歉!

最佳答案

这是你需要做的才能让它正确:

void add(struct database* test)
{
    printf("\nPlease enter a 5 digit employee number: ");
    scanf("%s",test->ID);
}

int main()
{
    ...
    int a;
    struct database employee[20];
    for (a=0; a<sizeof(employee)/sizeof(*employee); a++)
        add(&employee[a]); // or add(employee+a);
    ...
}

关于c - 在 C 中传递结构数组的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23060128/

相关文章:

jquery .index() 函数返回不正确

c - 程序(数组到结构)警告 : assignment makes integer from pointer without a cast

c - 如何从此函数中删除多个返回

c - 当我从进程内部检查进程的 CPU 使用率时,top 命令返回 0% CPU 使用率

c - printf() 函数如何处理使用全局缓冲区的函数返回的字符串?

使用不同的键进行 JSON 解码

c - 将 void* 变量分配给结构变量

c - 终端终止后窗口立即关闭

c++ - 绕过#define'd 宏?

c++ - 我已经创建了一个模板函数,但在检查输入是奇数还是偶数时出现错误 C++