gpt4 book ai didi

c - Getcontext() 适用于具有结构的数组元素

转载 作者:行者123 更新时间:2023-11-30 18:01:00 24 4
gpt4 key购买 nike

typedef struct _ut_slot {
ucontext_t uc;
....
}*ut_slot;

static ut_slot* table; //array of the structs

void foo (int tab_size){
table = malloc ( tab_size *(sizeof (ut_slot))); // memory allocation for array of structs
for(i = 0 ; i < tab_size ; i++ ){
getcontext(&table[i].uc); <--- ??????
}
}

我在“getcontext”字符串中收到错误。如何编写对数组任何元素的引用?如何使用“getcontext”命令初始化每个数组元素的“uc”字段?

最佳答案

您对 ut_slot 的定义及其使用不一致:

typedef struct _ut_slot {
ucontext_t uc;
....
}*ut_slot; //<--- pointer to struct

你说ut_slot是一个指向结构的指针,然后你声明

static ut_slot* table;

这样你就有了一个指向结构体的指针。

您可能希望 ut_slot 只是一个结构,或者 table 是一个指向结构的指针。

<小时/>

更准确地说:table 是一个指向结构体指针的指针,因此 table[i] 是一个指向结构体的指针,并且您尝试使用 table[i].ut 访问非结构体的结构体成员,这会引发编译错误。

<小时/>

尝试以下操作:

typedef struct _ut_slot {
ucontext_t uc;
....
} ut_slot; //removed "*"

static ut_slot *table;

其余代码没问题,不需要更改。

关于c - Getcontext() 适用于具有结构的数组元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10047993/

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