Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 了解如何动态创建结构数组并访问其元素_C_Pointers_Structure - Fatal编程技术网

C 了解如何动态创建结构数组并访问其元素

C 了解如何动态创建结构数组并访问其元素,c,pointers,structure,C,Pointers,Structure,我需要将指向结构的指针的地址传递给函数,该函数将动态地为结构数组分配内存并填充值 现在从我的调用方法来看,一旦我从func1返回,我应该能够遍历结构数组并显示结构变量的值 有人能解释一下如何将指针的地址传递给结构,同时遍历动态创建的结构数组吗 我的示例代码如下所示: struct test { int a; int b; }; void func1(int *n,struct test **testobj) { n=5; *testobj = (struct

我需要将指向结构的指针的地址传递给函数,该函数将动态地为结构数组分配内存并填充值

现在从我的调用方法来看,一旦我从func1返回,我应该能够遍历结构数组并显示结构变量的值

有人能解释一下如何将指针的地址传递给结构,同时遍历动态创建的结构数组吗

我的示例代码如下所示:

struct test {
    int a;
    int b;
};

void func1(int *n,struct test **testobj)

{
    n=5;
    *testobj = (struct test*) malloc(n*sizeof(struct test));
    for(i=0;i<n;i++)
    {
        (*testobj)[i].a=1;
        (*testobj)[i].b=2;
    }
}

int main()
{
    struct test testobj;int n;
    func1(&n,&testobj);
    for(i=0;i<n;i++)
    {
        printf("%d %d",(*testobj)[i].a,*testobj)[i].b);
    }
    free(testobj);
}
struct测试{
INTA;
int b;
};
void func1(int*n,结构测试**testobj)
{
n=5;
*testobj=(结构测试*)malloc(n*sizeof(结构测试));

对于(i=0;i在声明步骤本身中创建指向结构的指针数组,并将其传递给函数

struct test *testobj[10];
func1(&n,testobj);
这会将整个指针数组传递给main()中的函数

,定义指向
测试
结构的指针:

struct test *testobj[10];
func1(&n,testobj);
struct test *testPtr;
要获取该指针的地址,请使用操作员的
&
地址:

&testPtr;
这将返回指针的地址,并具有类型
struct test**

然后,您可以将其传递到函数
func1
,该函数执行正确的分配(尽管强制执行
malloc()
通常被认为是不好的做法)

*testobj = malloc(n*sizeof(struct test));
…是正确的。
*testobj
取消引用通过执行
&testPtr
获得的双指针,并将新内存的地址存储在指针中。使用
(*testobj)[i]
取消引用双指针也是正确的,因为
[]
的优先级高于
*
,您需要(正如您所做的那样)将取消引用用括号括起来,以确保在获取索引之前发生这种情况

因此,当
func1()
返回指针
testPtr
时,指针现在应该指向您分配的
n
test
结构的数组,并且可以使用
testPtr[i].a
等进行访问

编辑:您的for循环应为

for(i=0;i<n;i++)
    printf("%d %d", testobj[i].a, testobj[i].b);

我觉得你的代码很好。 只有编辑,应该使其罚款是--

代替

struct test testobj;
输入以下代码

struct test  *testobj;
剩下的就保持原样吧

这是所需功能的工作版本,在这里,内存是根据需要在被调用函数中分配的

#include <stdlib.h>
#include <stdio.h>
struct tests {
    int a;
    int b;
};

void func1(int *n,struct tests **testobj)

{
    int i;
    *n=5;
    *testobj = (struct tests*) malloc((*n)*sizeof(struct tests));
    for(i=0;i<(*n);i++)
    {
        (*testobj)[i].a=1;
        (*testobj)[i].b=2;
    }
}

int main()
{
    int i;
    struct tests *testobj;int n;
    func1(&n,&testobj);
    for(i=0;i<(n);i++)
    {
        printf("%d %d",(testobj)[i].a,testobj[i].b);
    }
    free(testobj);
}
#包括
#包括
结构测试{
INTA;
int b;
};
void func1(int*n,结构测试**testobj)
{
int i;
*n=5;
*testobj=(结构测试*)malloc((*n)*sizeof(结构测试));

对于(i=0;i来说,您所要求的版本并不完全清楚,但其中一个应该包括:

/* allocate some number of tests.
 *
 * out_n: out parameter with array count
 * returns: an array of tests
 */
struct test* allocate_some_tests(int *out_n) {
    int n = 5; /* hardcoded, random or otherwise unknown to caller */
    *out_n = n
    struct test *t = malloc(n * sizeof(*t));
    while (n--) {
        t[n].a = 1;
        t[n].b = 2;
    }
    return t;
}

/* allocate a specific number of tests.
 *
 * n: in parameter with desired array count
 * returns: an array of tests
 */
struct test* allocate_n_tests(int n) {
    struct test *t = malloc(n * sizeof(*t));
    while (n--) {
        t[n].a = 1;
        t[n].b = 2;
    }
    return t;
}
注意,您只需返回分配的数组,这里不需要指向指针的指针

至于调用它们并迭代结果:

void print_tests(struct test *t, int n) {
    for (; n--; t++)
        printf("{%d, %d}\n", t->a, t->b);
}

int main()
{
    int count1; /* I don't know how many yet */
    struct test *array1 = allocate_some_tests(&count1);
    print_tests(array1, count1);

    int count2 = 3; /* I choose the number */
    struct test *array2 = allocate_n_tests(count2);
    print_tests(array2, count2);
}

这不是所要求的。啊!是我从循环中访问结构元素弄乱了。现在清楚了!是的,n在这段代码中是不相关的。我需要它用于调试目的!非常感谢:)