修复与通用指针相关的警告

时间:2018-05-21 23:51:03

标签: c pointers void-pointers

我的主要功能是:

Data* countries_tail = NULL;
Data* countries_head = NULL;
readFile(countries_f, &countries_head, &countries_tail, &size, 0);

这是readFile的声明:

void readFile(FILE* f, void** head, void** tail, int* size, int city);

在readFile的第一行中,我调用一个将void指针强制转换为正确类型的函数。

castHeadTail(&head, &tail, city);

这是功能:

void castHeadTail(void*** head, void*** tail, int city){
    if (city){
        *head = (Cities**) (*head);
        *tail = (Cities**) (*tail);
    }
    else{
        *head = (Data**) (*head);
        *tail = (Data**) (*tail);
    }
}

在readFile中我也这样做:

if (city)
    readCities(head, tail, line);
else
    readCountries(head, tail, line, size);

这些是这些功能的声明:

void readCities(Cities** head, Cities** tail, char* line);
void readCountries(Data** head, Data** tail, char* line, int* size);

一切似乎都正常工作,valgrind说没有内存错误。但是,当我编译代码时,我收到了很多警告。以下是所有警告:

process.c: In function ‘readFile’:
process.c:96:17: warning: passing argument 1 of ‘readCities’ from incompatible pointer type [enabled by default]
                 readCities(head, tail, line);
                 ^
In file included from process.c:3:0:
process.h:105:6: note: expected ‘struct Cities **’ but argument is of type ‘void **’
 void readCities(Cities** head, Cities** tail, char* line);
      ^
process.c:96:17: warning: passing argument 2 of ‘readCities’ from incompatible pointer type [enabled by default]
                 readCities(head, tail, line);
                 ^
In file included from process.c:3:0:
process.h:105:6: note: expected ‘struct Cities **’ but argument is of type ‘void **’
 void readCities(Cities** head, Cities** tail, char* line);
      ^
process.c:98:17: warning: passing argument 1 of ‘readCountries’ from incompatible pointer type [enabled by default]
                 readCountries(head, tail, line, size);
                 ^
In file included from process.c:3:0:
process.h:91:6: note: expected ‘struct Data **’ but argument is of type ‘void **’
 void readCountries(Data** head, Data** tail, char* line, int* size);
      ^
process.c:98:17: warning: passing argument 2 of ‘readCountries’ from incompatible pointer type [enabled by default]
                 readCountries(head, tail, line, size);
                 ^
In file included from process.c:3:0:
process.h:91:6: note: expected ‘struct Data **’ but argument is of type ‘void **’
 void readCountries(Data** head, Data** tail, char* line, int* size);
      ^
process.c: In function ‘castHeadTail’:
process.c:199:15: warning: assignment from incompatible pointer type [enabled by default]
         *head = (Cities**) (*head);
               ^
process.c:200:15: warning: assignment from incompatible pointer type [enabled by default]
         *tail = (Cities**) (*tail);
               ^
process.c:203:15: warning: assignment from incompatible pointer type [enabled by default]
         *head = (Data**) (*head);
               ^
process.c:204:15: warning: assignment from incompatible pointer type [enabled by default]
         *tail = (Data**) (*tail);

我不知道如何修复这些警告,我不习惯使用通用指针,因为程序似乎工作正常,我根本不知道为什么他们在那里。

另外,抱歉没有包含演示问题的完整且可编译的示例,我不知道如果代码似乎没有任何问题,如何显示问题。

1 个答案:

答案 0 :(得分:-1)

我修改了所有警告,删除了castHeadTail()按照建议并切换了所有void**void***。所以现在它看起来像这样:

void readFile(FILE* f, void* head, void* tail, int* size, int city);

然后readFile中的第一件事就是:

if (city){
    head = (Cities**) head;
    tail = (Cities**) tail;
}
else{
    head = (Data**) head;
    tail = (Data**) tail;
}

就是这样。代码仍然可以正常工作,它编译时没有任何警告,我只是想知道我是否仍在调用任何未定义的行为。