gpt4 book ai didi

c - 通过函数传递动态数组结构

转载 作者:行者123 更新时间:2023-12-02 07:12:47 25 4
gpt4 key购买 nike

struct aPoint {
int somaVertical;
int somaHorizontal;
int valor;
};

我有一个在 main() 中动态创建的结构数组,如下所示:

struct aPoint *ps = malloc( sizeof(struct aPoint) * columns * rows )

我想在具有 sscanf() 的函数中使用它在 main() 之外的 struct 值。数组的初始化也在 main() 上处理

如何通过该函数传递结构数组并设置它的一些结构值?啊,我讨厌指点!

谢谢!

最佳答案

那就是:

    int readStuff(struct aPoint *ps, size_t len, const char *someVar)
{
unsigned int i;
for (i = 0; i < len; i++) {
sscanf(someVar, "%d", &(ps[i].somaVertical));
/* And so on for the other fields */
}
/* Return whatever you're returning here */
}

const size_t len = colunas * linhas;
struct aPoint *ps = calloc(len, sizeof(struct aPoint));
int success = readStuff(ps, len, arrayOfNumbers);

关于c - 通过函数传递动态数组结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118647/

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