C语言 c指向结构数组的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16201607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:10:08  来源:igfitidea点击:

c pointer to array of structs

carrayspointersstruct

提问by user1539348

I know this question has been asked a lot, but I'm still unclear how to access the structs.

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

I want to make a global pointer to an array of structs:

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

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;
}

But it gives me warnings. How should I access the specific structs with []?

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

Similarly, how should I create an array of pointers of struct type? test_t *_array_ptr[2];?

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

回答by Adam Rosenfield

The syntax you are looking for is somewhat cumbersome, but it looks like this:

你要找的语法有点麻烦,但看起来像这样:

// Declare test_array_ptr as pointer to array of test_t
test_t (*test_array_ptr)[];

You can then use it like so:

然后你可以像这样使用它:

test_array_ptr = &array_t1;
(*test_array_ptr)[0] = new_struct;

To make the syntax easier to understand, you can use a typedef:

为了使语法更容易理解,您可以使用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;

The cdeclutility is useful for deciphering complex C declarations, especially when arrays and function pointers get involved.

CDECL实用程序是破译复杂的C声明是有用的,尤其是当数组和函数指针参与进来。

回答by LihO

test_t * test_array_ptris a pointer to test_t. It could be a pointer to single instance of test_t, but it could be a pointer to the first element of an array of instances of test_t:

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

test_t array1[1024];

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

this makes myArraypoint to the first element of array1and pointer arithmetic allows you to treat this pointer as an array as well. Now you could access 2nd element of array1like this: myArray[1], which is equal to *(myArray + 1).

myArray指向第一个元素,array1并且指针算术允许您将此指针视为数组。现在您可以array1像这样访问第二个元素:myArray[1],它等于*(myArray + 1)

But from what I understand, what you actually want to do here is to declare a pointer to pointer to test_tthat will represent an array of pointers to arrays:

但据我所知,您在这里真正想要做的是声明一个指向指针的指针,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];

回答by K Scott Piel

The issue you have is that you are taking (*test_array_pointer)which is the first element of the array. If you want to assign to a specific element of the array, you would do the following...

您遇到的问题是您正在使用(*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 ) );
}

if you want to always assign to the first element of the array you could do this...

如果你想总是分配给数组的第一个元素,你可以这样做......

function foo()
{
    test_array_ptr = array_t1;

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

and has been pointed out to me by others, and something I honestly had entirely forgotten for having not used it in the better part of forever, you can do direct assignment of simple structures in C...

并且已经被其他人向我指出,老实说我已经完全忘记了因为在大部分时间里没有使用它,你可以在 C 中直接分配简单的结构......

function foo()
{
    test_array_ptr = array_t1;

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

回答by Sheldon Juncker

I would use a pointer to a pointer like:

我会使用一个指向指针的指针,如:

test_t array_t1[1024];
test_t **ptr;
ptr = array_t1;
ptr[0] = ...;
ptr[1] = ...;
etc.