gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:20:59 26 4
gpt4 key购买 nike

我想了解如何通过引用将结构数组传递给从第一个函数中调用/执行的第二个函数。我的目标是第二个函数修改/更改任意结构的内容。下面的代码可以工作,但不幸的是,它并没有完全达到我想要实现的目标。我希望能够访问第二个函数中的任意结构。换句话说,我想通过在 中调用/执行 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com