C指向结构体数组的指针

20

我知道这个问题已经被问了很多次,但我仍然不清楚如何访问结构体。

我想创建一个指向结构体数组的全局指针:

typdef struct test
{
    int obj1;
    int obj2;
} test_t;

extern test_t array_t1[1024];
extern test_t array_t2[1024];
extern test_t array_t3[1025];

extern test_t *test_array_ptr;

int main(void)
{
    test_array_ptr = array_t1;

    test_t new_struct = {0, 0};
    (*test_array_ptr)[0] = new_struct;
}

但它给我警告。我应该如何使用[]访问特定的结构体?

同样,我应该如何创建结构体类型指针数组?test_t *_array_ptr[2];


1
不应该给你警告,而应该给出一个错误。 (*test_array_ptr)[0] 解引用了两次,但是只有一级星号。 - Daniel Fischer
@DanielFischer:例如,gcc 经常为“约束违规”构造打印警告(这是 C 语言所能表达的“非法”的最接近说法)。-pedantic-errors 选项使其行为更加严格。 - Keith Thompson
@KeithThompson 是的,但对于那个特定的问题,gcc会在没有任何标志的情况下说deref.c:18:18: error: subscripted value is neither array nor pointer nor vector。(嗯,什么是“vector”,这是C语言吗?)对于像解引用结构体或访问int成员之类的东西,编译器根本不知道该如何处理,它放弃并抛出一个错误。 - Daniel Fischer
4个回答

29

您正在寻找的语法有些繁琐,但它看起来是这样的:

// Declare test_array_ptr as pointer to array of test_t
test_t (*test_array_ptr)[];
您可以按照以下方式使用它:
test_array_ptr = &array_t1;
(*test_array_ptr)[0] = new_struct;
为了使语法更易于理解,您可以使用 typedef
// Declare test_array as typedef of "array of test_t"
typedef test_t test_array[];
...
// Declare test_array_ptr as pointer to test_array
test_array *test_array_ptr = &array_t1;
(*test_array_ptr)[0] = new_struct;

cdecl 工具对于解读复杂的C语言声明非常有用,特别是当涉及数组和函数指针时。


谢谢你的帮助回答!但第一个回答和这个有什么区别呢?函数foo() { test_array_ptr = array_t1;test_t new_struct = {0,0}; test_array_ptr[0] = new_struct;} - user1539348
1
请展示如何使用 -> 运算符访问 (*test_array_ptr)[1].obj1。这是否可行?我知道 test_array_ptr[1]->obj1 会给我错误的结果。 - Evgeny

5

test_t * test_array_ptr是指向test_t的指针。它可以是指向单个test_t实例的指针,也可以是指向test_t实例数组的第一个元素的指针:

test_t array1[1024];

test_t *myArray;
myArray= &array1[0];

这将使myArray指向array1的第一个元素,指针算术允许您将此指针视为数组。现在,您可以像这样访问array1的第二个元素:myArray [1],它等同于*(myArray + 1)
但据我所知,您实际上想要做的是声明一个指向指向test_t的指针的指针,表示指针数组到数组:
test_t array1[1024];
test_t array2[1024];
test_t array3[1025];

test_t **arrayPtr;
arrayPtr = malloc(3 * sizeof(test_t*));   // array of 3 pointers
arrayPtr[0] = &array1[0];
arrayPtr[1] = &array2[0];
arrayPtr[2] = &array3[0];

我应该如何一次性创建一个指向单个数组结构体的指针?我应该如何访问数组中的结构体? - user1539348

0
你遇到的问题是你正在获取数组的第一个元素(*test_array_pointer)。如果你想要给数组中的特定元素赋值,你需要执行以下操作...
function foo()
{
    test_array_ptr = array_t1;

    test_t new_struct = {0,0};
    memcpy( &test_array_ptr[0], &new_struct, sizeof( struct test_t ) );
}

如果你想总是分配给数组的第一个元素,你可以这样做...
function foo()
{
    test_array_ptr = array_t1;

    test_t new_struct = {0,0};
    memcpy( test_array_ptr, &new_struct, sizeof( struct test_t ) );
}

这是其他人指出的,而我也因为很久没有使用它而完全忘记了,你可以在C语言中直接赋值简单结构。

function foo()
{
    test_array_ptr = array_t1;

    test_t new_struct = {0,0};
    test_array_ptr[0] = new_struct;
}

1
我非常确定你可以通过简单地使用 = 来复制结构体(而不是指向结构体的指针)。 - user1944441
1
可以的。例如,一种简单的复制数组的方法是将它们包装在一个“结构体”中,并进行简单的赋值。 - Daniel Fischer
是的...那真是个愚蠢的错误...只是我几十年没用过它了,甚至忘记了你还能这样做。已经修复了。 - K Scott Piel

0
我会使用一个指向指针的指针,例如:
test_t array_t1[1024];
test_t **ptr;
ptr = array_t1;
ptr[0] = ...;
ptr[1] = ...;
etc.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接