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

标签 c arrays malloc

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/

相关文章:

c - 当可移植性最重要时使用 C 的 SDL 与 SFML?

c - 取消引用数组

arrays - (算法)在不排序的情况下,在 Θ(n*logn) 时间内查找两个未排序的数组是否有共同元素?

c - 为堆中的结构元素分配内存?

c - 整数数组的深拷贝,仅使用指针算法

c - 已分配的内存 - 已分配数组的结构

c - 餐饮哲学家的错误

c - 使用 setrlimit 设置线程的堆栈大小

javascript - 如何从对象数组中获取特定数据并将其存储在js(vue js)中的新数组中

javascript - mojolicious 将数组分配给 JavaScript 变量