簡體   English   中英

在C中取消引用'void *'指針

[英]dereferencing ‘void *’ pointer in C

所以我把那些放在.h中

stable.h

// The symbol table.
typedef struct stable_s *SymbolTable;

// Data stored.
typedef union {
  int i;
  char *str;
  void *p;
} EntryData;

// Return struct for stable_insert.
typedef struct {
  int new;  // Was a new entry created?
  EntryData *data;  // Data associated with entry.
} InsertionResult;

etc

然后在我的.c

stable.c

etc

typedef struct {
    char *key;
    EntryData data;
} Entry;

struct stable_s { /* Indice hash */
    char index;
    int size;
    void *pointer;
};

etc

EntryData *stable_find(SymbolTable table, const char *key){
    int k;
    char c;

    if (key[0] >= 'A' && key[0] <= 'Z')
        c = table[(key[0] - 65)].index;
    else if (key[0] >= 'a' && key[0] <= 'z')
        c = table[(key[0] - 141)].index;
    else
        c = table[26].index;

    c -= 65;

    if (table[c].size == 0)
        return NULL;

    k = busca_binaria(table[c].pointer, table[c].size, key);
    if (strcmp(table[c]->pointer[k]->key, key) == 0)
        return table[c]->pointer[k]->data;

    return NULL;
}

gcc -std=c99 stable.c -o stable給了我這些錯誤:

 stable2.c: In function 'stable_find': stable2.c:128:24: error: invalid type argument of '->' (have 'struct stable_s') if (strcmp(table[c]->pointer[k]->key, key) == 0) stable2.c:129:24: error: invalid type argument of '->' (have 'struct stable_s') return table[c]->pointer[k]->data; 

我想念什么? 我真的迷上了語法。

讓我們看一下這些行:

k = busca_binaria(table[c].pointer, table[c].size, key);
if (strcmp(table[c]->pointer[k]->key, key) == 0)
    return table[c]->pointer[k]->data;

什么是table struct stable_s *

所以

table[c]

是一個struct stable_s -它不是指針。

所以回到行:

k = busca_binaria(table[c].pointer, table[c].size, key);
                  ^^^^^^^^^^^^^^^^
                  Correct as table[c] is a struct stable_s

if (strcmp(table[c]->pointer[k]->key, key) == 0)
           ^^^^^^^^^^^^^^^^^^^^
           Wrong as table[c] is not a point

    return table[c]->pointer[k]->data;
           ^^^^^^^^^^^^^^^^^^^^
           Wrong as table[c] is not a point

您的代碼中似乎還有其他問題(例如,對`void *的取消引用),但以上問題應與編譯器錯誤有關。

也許這行:

if (strcmp(table[c]->pointer[k]->key, key) == 0)

應該:

if (strcmp(((Entry*)table[c].pointer)[k].key, key) == 0)

但是我不確定,因為您還沒有發布說明這一點的代碼。

也許這行:

return table[c]->pointer[k]->data;

應該:

return &(((Entry*)table[c].pointer)[k].data);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM