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

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 に答える
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 に答える