0
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”字段?

4

2 回答 2

0

getcontext 的手册提出了这样的原型:

int getcontext(ucontext_t *ucp);

你确定你传递的是正确的论点吗?;)

如果你真的想要一个结构数组,你必须为每个结构留出一些内存。注意 -> 如何取消引用指针并 & 获取成员结构的地址。它令人困惑,向后看。

抱歉,我的程序没有大括号和标点符号。锚点 C 自动添加它们。

#include <stdio.h>
#include <stdlib.h>
#include <ucontext.h>
typedef struct _ut_slot
    ucontext_t uc
    ...
*ut_slot
static ut_slot *table
int main  void
    static ucontext_t uc
    table = malloc  1 * sizeof ut_slot
    table[0] = malloc  sizeof *ut_slot
    getcontext  &table[0]->uc
    free  table[0]
    free  table
    return 0
于 2012-04-06T19:10:56.637 回答
0

您对它的定义和使用有不一致ut_slot的地方:

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

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

静态 ut_slot* 表;

所以你有一个指向结构的指针。

您可能只想ut_slot成为一个结构,或者table成为一个指向结构的指针。


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


尝试以下操作:

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

static ut_slot *table;

您的其余代码都可以,不需要更改。

于 2012-04-06T19:09:48.407 回答