c - 将结构数组传递给函数?

标签 c arrays function struct

我想了解如何通过引用将结构数组传递给从第一个函数中调用/执行的第二个函数。我的目标是第二个函数修改/更改任意结构的内容。下面的代码可以工作,但不幸的是,它并没有完全达到我想要实现的目标。我希望能够访问第二个函数中的任意结构。换句话说,我想通过在 中调用/执行 first function 来处理 第二个函数 中的所有结构(使用 for 循环) main一次并且不使用for循环。

下面代码中的第二个函数被命名为passByReference_inner

array_of_struct.h:

struct card
{
    int face;
    int nose;
};

typedef struct card HEAD ;

/* prototype */
extern void passByReference(HEAD **c);      /* first function */
extern void passByReference_inner(HEAD *c); /* second function */

第一个函数:(passByReference)

#include <stdio.h>
#include "array_of_struct.h"

void passByReference(HEAD **c)
{
    passByReference_inner (*c);   /* second function */
}

第二个函数: (passByReference_inner)

#include <stdio.h>
#include "array_of_struct.h"

void passByReference_inner(HEAD *c)
{
    c->face = (c->face) + 1000;
    c->nose = (c->nose) + 2000;
}

主要:

#include <stdio.h>
#include "array_of_struct.h"

int main(void)
{
    int             i;
    static HEAD     c[12];
    static HEAD *cptr[12];

    for ( i = 0; i < 12; i++ )
    {
        c[i].face = i + 30;
        c[i].nose = i + 60; 
        cptr[i]   = &c[i];
    }

    for ( i = 0; i < 12; i++ )
    {
        passByReference(&cptr[i]);  /* first function */
    }
    return 0;
}

最佳答案

我认为你想做的是这个

#include <stdio.h>

struct card
{
    int face;
    int nose;
};

typedef struct card HEAD ;

/* prototype */
void passByReference(HEAD *c, int count);      /* first function */
void passByReference_inner(HEAD *c); /* second function */

void passByReference(HEAD *c, int count)
{
    int i;
    for (i = 0 ; i < count ; i++)
        passByReference_inner (&(c[i]));   /* second function */
}

void passByReference_inner(HEAD *c)
{
    c->face = (c->face) + 1000;
    c->nose = (c->nose) + 2000;
}

int main(void)
{
    int  i;
    HEAD c[12]; /* you don't need static here (do you know what static is for?) */

    for ( i = 0; i < 12; i++ )
    {
        c[i].face = i + 30;
        c[i].nose = i + 60;
    }
    /* 
     * the element count of the array is sizeof(c) / sizeof(c[0]) 
     *    (totalSizeOfArray) / (indivudualElementSizeOfArray).
     */
    passByReference(c, sizeof(c) / sizeof(c[0]));  /* first function */

    return 0;
}

你应该知道 c 中的数组当作为参数传递给函数时,衰减为指向其第一个元素的指针。

由于您想处理第二个函数中的所有结构,因此我认为不需要第一个函数,无论如何,这就是您的做法

#include <stdio.h>

struct card
{
    int face;
    int nose;
};

typedef struct card HEAD ;

/* prototype */
void passByReference(HEAD *const c, int count);      /* first function */
void passByReference_inner(HEAD *const c, int count); /* second function */

void passByReference(HEAD *const c, int count)
{
    passByReference_inner(c, count);   /* second function */
}

/* HEAD *const c prevents the pointer c to be changed
 * this way it will never point anywhere else.
 *
 * And you can be sure to alter the original data.
 */
void passByReference_inner(HEAD *const c, int count)
{
    for (int i = 0 ; i < count ; ++i)
    {
        c[i].face = (c[i].face) + 1000;
        c[i].nose = (c[i].nose) + 2000;
    }
}

int main(void)
{
    int  i;
    HEAD c[12]; /* you don't need static here (do you know what static is for?) */

    for ( i = 0; i < 12; i++ )
    {
        c[i].face = i + 30;
        c[i].nose = i + 60;
    }
    /* 
     * the element count of the array is sizeof(c) / sizeof(c[0]) 
     *    (totalSizeOfArray) / (indivudualElementSizeOfArray).
     */
    passByReference(c, sizeof(c) / sizeof(c[0]));  /* first function */

    return 0;
}

由于您实际上传递了一个指针,因此您可以直接在第一个和第二个函数中更改它的内容。

还有一件事,您实际上并不需要 static 关键字,特别是在 main() 中,static 会保留函数调用之间的变量,并且由于 main() 通常在程序的生命周期中只会被调用一次...在那里使用 static 没有多大意义.

关于c - 将结构数组传递给函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27673700/

相关文章:

ios - 从单 View 应用程序 (objective-c) 调用以函数作为 Cocoa Touch Framework (swift) 参数的方法

c - 在 OS X Mavericks 上安装 libsndfile 时出现 fatal error

C 无法检索 mongodb BSON 数组

javascript - 数组中的值被覆盖

php - 在文件名数组中搜索以 ".txt"结尾的文件

javascript - 返回函数表达式而不是结果

c - Strncpy 和 char **

c - Keil 中的 Traffic Light for Tiva 系列 - c 编程

arrays - Postgres SQL 函数转换可以应用于数组类型吗?

javascript - JS - 使用 "new"和将对象作为上下文传递给构造函数有什么区别