【问题标题】:dereferencing ‘void *’ pointer in C在C中取消引用'void *'指针
【发布时间】:2016-10-06 19:03:30
【问题描述】:

所以我有那些在 .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;

我错过了什么?我真的迷失了语法。

【问题讨论】:

  • SymbolTable 是什么?
  • 你的编译器默认的char类型是unsigned char吗?因为这个表达式key[0] - 141
  • @EugeneSh.:参见上面的 stable.h:typedef struct stable_s *SymbolTable;
  • @FredLarson 对。我的 Ctrl-F 出卖了我……
  • 无论如何。 table[whatever] 是结构体,不是指针,所以不要在上面使用-&gt;..

标签: c void-pointers dereference


【解决方案1】:

让我们看看这些行:

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);

【讨论】:

  • 然后我得到这些错误: stable2.c:128:33: 警告:取消引用 'void *' 指针 if (strcmp((table[c].pointer[k]->key), key ) == 0) stable2.c:128:5: error: void value 没有被忽略,因为它应该是 if (strcmp((table[c].pointer[k]->key), key) == 0) stable2 .c:129:32:警告:取消引用'void *'指针返回表[c].pointer[k]->data; stable2.c:129:9: 错误: void value 不被忽略,因为它应该是 return table[c].pointer[k]->data;
  • @Paul - 没错,你不能取消引用 void* - 你需要知道原始指针的类型并转换回那个类型。你如何初始化pointer
  • 我只在需要时在 stable_insert 函数中初始化它
  • if (table[c].size == 0) { table[c].pointer = malloc (sizeof (Entry*));表[c].size++; k = 0; }
  • 指针应该是Entry **pointer,因为它指向Entry *data?
猜你喜欢
  • 2012-05-23
  • 2011-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多