【问题标题】:How can I pass an array of structures to a function by reference? [duplicate]如何通过引用将结构数组传递给函数? [复制]
【发布时间】:2016-11-06 05:09:00
【问题描述】:
#include <stdio.h>

void changeValues(struct ITEM *item[]);

struct ITEM
{
int number;
};

int main(void)
{
    struct ITEM items[10];
    for (int i = 0; i < 10; i++)
    {
        items[i].number = i;//initialize
        printf("BEFORE:: %d\n", items[i].number);
    }

    changeValues(items);

    for (int i = 0; i < 10; i++)
    {
        items[i].number = i;
        printf("AFTER:: %d\n", items[i].number);
    }

    return 0;
}

void changeValues(struct ITEM *item[])
{
    for (int i = 0; i < 10; i++)
    item[i] -> number += 5;
}

我正在尝试将结构数组传递给函数。我需要通过引用而不是值来更改函数内结构成员的值。由于某些奇怪的原因,当我在调用函数后打印结果时,值保持与函数调用之前相同。

【问题讨论】:

    标签: c function structure pass-by-reference


    【解决方案1】:

    在 C 中,您不能通过引用传递(如 C++)。只能传值,也可以传指针。

    在这种情况下,您似乎希望将结构数组传递给函数changeValues。这就是您在main 中所做的事情。但是,changeValues 的原型和实现实际上是尝试将指针数组传递给 struct ITEM

    一种可能的解决方法是将指向结构ITEM 的指针数组更改为结构数组。

    void changeValues(struct ITEM item[])
    {
        for (int i = 0; i < 10; i++)
        {
            item[i].number += 5;
        }
    }
    

    编辑:您的代码中实际上还有另外两个错误:

    1) struct ITEM 的定义需要在changeValues 原型之前:

    struct ITEM
    {
        int number;
    };
    
    void changeValues(struct ITEM item[]);
    

    2) 在您的 main() 中,您实际上重置了 changeValues 中的所有值 - 基本上您使该函数中所做的一切无效:

    for (int i = 0; i < 10; i++)
    {
        items[i].number = i;   // <-- Remove this line as you are resetting the value again here
        printf("AFTER:: %d\n", items[i].number);
    }
    

    struct ITEM
    {
        int number;
    };
    
    void changeValues(struct ITEM item[]);
    
    
    int main(void)
    {
        struct ITEM items[10];
        for (int i = 0; i < 10; i++)
        {
            items[i].number = i;//initialize
            printf("BEFORE:: %d\n", items[i].number);
        }
    
        changeValues(items);
    
        for (int i = 0; i < 10; i++)
        {
            // items[i].number = i;   // <-- Remove this line as you are resetting the value again here
            printf("AFTER:: %d\n", items[i].number);
        }
    
        return 0;
    }
    
    void changeValues(struct ITEM items[])
    {
        for (int i = 0; i < 10; i++)
        {
            items[i].number += 5;
        }
    }
    

    【讨论】:

    • 在我的 main 中调用该函数时,它看起来像“changeValues(item);”吗? ?
    • 谢谢。最佳答案。我的程序正在运行,我按时提交了作业。
    【解决方案2】:

    您可以通过传递指针地址来“作为引用传递”。

    这是一个例子:

    int main(void)
    {
    
        char *pA;     /* a pointer to type character */
        char *pB;     /* another pointer to type character */
        puts(strA);   /* show string A */
        pA = strA;    /* point pA at string A */
        puts(pA);     /* show what pA is pointing to */
        pB = strB;    /* point pB at string B */
        putchar('\n');       /* move down one line on the screen */
        while(*pA != '\0')   /* line A (see text) */
        {
            *pB++ = *pA++;   /* line B (see text) */
        }
        *pB = '\0';          /* line C (see text) */
        puts(strB);          /* show strB on screen */
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-30
      • 2018-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-02
      • 2020-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多