gpt4 book ai didi

c - C 处的特殊警告

转载 作者:行者123 更新时间:2023-12-02 21:34:12 27 4
gpt4 key购买 nike

在我的项目中我有这两个文件。当我尝试编译它时,出现一些我无法解决的特殊警告,尽管我的程序运行良好。我在编译时使用 -Wall 和 -Wextra 参数。

//File: DataStructures.h 
typedef struct EntryListPacketStruct EntryListPacket;
typedef struct IndexesPacketStruct IndexesPacket;
typedef struct MatchingQueriesResultStruct MatchingQueriesResult;
typedef struct DataPacketStruct* DataPacketPtr;

extern DataPacketPtr Packet;

EntryListPacket* allocateEntryListPacket(void);

void initializeDataPacket(DataPacketPtr);
void freeDataPacket(DataPacketPtr);

void initializeEntryListPacket(EntryListPacket*);
void freeEntryListsPacket(EntryListPacket*);

我将 DataStructures.h 包含在以下文件中:

//File: DataStructures.c
struct EntryListPacketStruct {
EntryList* exactTypeEntryList;
EntryList** editDistEntryLists;
EntryList** hammingDistEntryLists;
};

struct DataPacketStruct {
struct EntryListPacket* entryLists;
struct IndexesPacket* indexes;
dllistptr matchingQResultList;
};

EntryListPacket* allocateEntryListPacket(void){
EntryListPacket* temp=malloc(sizeof(EntryListPacket));
return temp;
}

void initializeDataPacket(DataPacketPtr packet){
DataPacketPtr temp;

temp=malloc(sizeof(struct DataPacketStruct));

initializeEntryListPacket(temp->entryLists);

initializeIndexesPacket(temp->indexes);

packet=temp;
}

void freeDataPacket(DataPacketPtr packet){

freeEntryListsPacket(packet->entryLists);
freeIndexesPacket(packet->indexes);
free(packet);
}

void initializeEntryListPacket(EntryListPacket* packet) {

packet->exactTypeEntryList = NULL;

packet->editDistEntryLists = malloc(sizeof (EntryList)*22);
int i;
for (i = 0; i < 22; i++) {
packet->editDistEntryLists[i] = NULL;
}


packet->hammingDistEntryLists = malloc(sizeof (EntryList)*27);
for (i = 0; i < 27; i++) {
packet->hammingDistEntryLists[i] = NULL;
}
}

void freeEntryListsPacket(EntryListPacket* packet){
if(packet->exactTypeEntryList!=NULL)
DestroyEntryList(packet->exactTypeEntryList);
free(packet->exactTypeEntryList);

int i;
for(i=0;i<22;i++){
if(packet->editDistEntryLists[i]!=NULL){
printf("%d\n",i);
DestroyEntryList(packet->editDistEntryLists[i]);

}
free(packet->editDistEntryLists[i]);
}
free(packet->editDistEntryLists);

for(i=0;i<27;i++){
if(packet->hammingDistEntryLists[i]!=NULL){
printf("%d\n",i);
DestroyEntryList(packet->hammingDistEntryLists[i]);
}
free(packet->hammingDistEntryLists[i]);
}
free(packet->hammingDistEntryLists);
free(packet);
}

//文件:main.c

EntryListPacket *temp;
temp=allocateEntryListPacket();
initializeEntryListPacket(temp);

freeEntryListsPacket(temp);

我收到以下警告:

 DataStructures.c: In function ‘initializeDataPacket’:
DataStructures.c:48:6: warning: passing argument 1 of ‘initializeEntryListPacket’ from incompatible pointer type [enabled by default]
initializeEntryListPacket(temp->entryLists);
^
In file included from DataStructures.c:6:0:
DataStructures.h:27:10: note: expected ‘struct EntryListPacket *’ but argument is of type ‘struct EntryListPacket *’
void initializeEntryListPacket(EntryListPacket*);
^
DataStructures.c:50:6: warning: passing argument 1 of ‘initializeIndexesPacket’ from incompatible pointer type [enabled by default]
initializeIndexesPacket(temp->indexes);
^
In file included from DataStructures.c:6:0:
DataStructures.h:30:10: note: expected ‘struct IndexesPacket *’ but argument is of type ‘struct IndexesPacket *’
void initializeIndexesPacket(IndexesPacket*);
^
DataStructures.c:43:42: warning: parameter ‘packet’ set but not used [-Wunused-but-set-parameter]
void initializeDataPacket(DataPacketPtr packet){
^
DataStructures.c: In function ‘freeDataPacket’:
DataStructures.c:57:6: warning: passing argument 1 of ‘freeEntryListsPacket’ from incompatible pointer type [enabled by default]
freeEntryListsPacket(packet->entryLists);
^
In file included from DataStructures.c:6:0:
DataStructures.h:28:10: note: expected ‘struct EntryListPacket *’ but argument is of type ‘struct EntryListPacket *’
void freeEntryListsPacket(EntryListPacket*);
^
DataStructures.c:58:6: warning: passing argument 1 of ‘freeIndexesPacket’ from incompatible pointer type [enabled by default]
freeIndexesPacket(packet->indexes);
^
In file included from DataStructures.c:6:0:
DataStructures.h:31:10: note: expected ‘struct IndexesPacket *’ but argument is of type ‘struct IndexesPacket *’
void freeIndexesPacket(IndexesPacket*);

我无法弄清楚我做错了什么,或者编译器是否被卡住,因为例如:DataStructures.h:28:10: note: Expected 'struct EntryListPacket *' but argument is of type 'struct EntryListPacket *'对我来说似乎不合理。提前谢谢!

最佳答案

您的 struct 标记包含 Struct 后缀,但您在声明时省略了它:

struct DataPacketStruct {
struct EntryListPacket* entryLists;
struct IndexesPacket* indexes;
dllistptr matchingQResultList;
};

应该是:

struct DataPacketStruct {
struct EntryListPacketStruct* entryLists;
struct IndexesPacketStruct* indexes;
dllistptr matchingQResultList;
};

或者只是:

struct DataPacketStruct {
EntryListPacket* entryLists;
IndexesPacket* indexes;
dllistptr matchingQResultList;
};

此外,您的 initializeDataPacket 函数正在泄漏内存。将 temp 分配给 packet 不会返回值,因为 packet 是按值传递的,而不是按引用传递的。

关于c - C 处的特殊警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21759454/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com