c-如何使用函数返回两个不同的链表



我正在以下代码中工作:

#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "gdal/gdal.h"
#include "gdal/cpl_conv.h"
#include <stdio.h>
#include <time.h>

typedef struct nlist{
char *data;
struct nlist *next;
}Node;
// Function to replace a string with another 
// string 
char* str_replace(char* string, const char* substr, const char* replacement) {
char* tok = NULL;
char* newstr = NULL;
char* oldstr = NULL;
int   oldstr_len = 0;
int   substr_len = 0;
int   replacement_len = 0;
newstr = strdup(string);
substr_len = strlen(substr);
replacement_len = strlen(replacement);
if (substr == NULL || replacement == NULL) {
return newstr;
}
while ((tok = strstr(newstr, substr))) {
oldstr = newstr;
oldstr_len = strlen(oldstr);
newstr = (char*)malloc(sizeof(char) * (oldstr_len - substr_len + replacement_len + 1));
if (newstr == NULL) {
free(oldstr);
return NULL;
}
memcpy(newstr, oldstr, tok - oldstr);
memcpy(newstr + (tok - oldstr), replacement, replacement_len);
memcpy(newstr + (tok - oldstr) + replacement_len, tok + substr_len, oldstr_len - substr_len - (tok - oldstr));
memset(newstr + oldstr_len - substr_len + replacement_len, 0, 1);
free(oldstr);
}
free(string);
return newstr;
}

Node* insert(Node*, char*);
void show(Node*);
/*
int getCount(Node *Head) 
{ 
int count = 0;  // Initialize count 
Node *current;
current = (Node *)malloc(sizeof(Node));
current = Head;  // Initialize current 
do {
count++;
}
while (current != NULL) 
{  
current = current->next; 
} 
return count; 
} 
*/
Node* insert(Node *Head, char *value)
{
Node *new_string;
new_string = (Node *)malloc(sizeof(Node));
new_string->data = malloc(strlen(value)+1);
strcpy(new_string->data,value);
Node *check;
check = (Node *)malloc(sizeof(Node));
if(Head == NULL){
Head = new_string;
Head->next = NULL;
}
else{
check = Head;
while(check->next != NULL)
check = check->next;
check->next = new_string;
new_string->next = NULL;
}
return Head;
}
void show(Node *Head)
{
Node *check;
check = (Node *)malloc(sizeof(Node));
check = Head;
if (check == NULL){
return;
}
while(check != NULL) {
printf("%s", check->data);
check=check->next;
}
printf("n");
}
//void listFilesRecursively(char *path, char *suffix);

int main()
{
char path[100];
char suffix[100];
// Input path from user
// Suffix Band Sentinel-2 of Type B02_10m.tif
printf("Enter path to list files: ");
scanf("%s", path);
printf("Enter the wildcard: ");
scanf("%s", suffix);
struct Node *B02List;
B02List = listFilesRecursively(path, suffix);
char *suffix_scl = "SCL_10m.tif";
struct Node *SCLList;
SCLList = listFilesRecursively(path, suffix_scl);
show(B02List);
show(SCLList);
return 0;
}

int string_ends_with(const char * str, const char * suffix)
{
int str_len = strlen(str);
int suffix_len = strlen(suffix);
return 
(str_len >= suffix_len) &&
(0 == strcmp(str + (str_len-suffix_len), suffix));
}
struct Node *listFilesRecursively(char *basePath, char *suffix)
{
char path[1000];
struct dirent *dp;
DIR *dir = opendir(basePath);
Node *Head = NULL;
Node *Head_scl = NULL;
if (!dir)
return;
while ((dp = readdir(dir)) != NULL)
{
if (strcmp(dp->d_name, ".") != 0 && strcmp(dp->d_name, "..") != 0)
{
strcpy(path, basePath);
strcat(path, "/");
strcat(path, dp->d_name);
if (string_ends_with(path, suffix))
Head = insert(Head, path);
listFilesRecursively(path, suffix);
}
} 
//show(Head);
closedir(dir);
return Head;
}

正如您所看到的,我将structNode放在***listFilesRecursive***函数中,因此我可以通过使用return Head返回类似的值(LInked-list(。我在main((中调用了两次这个函数,以查找具有两个不同结尾的文件。如果我只想打印列表,并且为此我将该函数的类型声明设置为void,那么这很好。但是,当我试图返回链表以便能够在main((函数中使用它们来打印它们时,我甚至无法编译代码。

在几个警告中,我得到了一个类型的错误

error: conflicting types for ‘listFilesRecursively’
struct Node *listFilesRecursively(char *basePath, char *suffix)

以下是构建警告和错误的完整列表:

list_dir_files_4.c: In function ‘main’:
list_dir_files_4.c:138:15: warning: implicit declaration of function ‘listFilesRecursively’ [-Wimplicit-function-declaration]
B02List = listFilesRecursively(path, suffix);
^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:138:13: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
B02List = listFilesRecursively(path, suffix);
^
list_dir_files_4.c:141:13: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
SCLList = listFilesRecursively(path, suffix_scl);
^
list_dir_files_4.c:142:10: warning: passing argument 1 of ‘show’ from incompatible pointer type [-Wincompatible-pointer-types]
show(B02List);
^~~~~~~
list_dir_files_4.c:105:6: note: expected ‘Node * {aka struct nlist *}’ but argument is of type ‘struct Node *’
void show(Node *Head)
^~~~
list_dir_files_4.c:143:10: warning: passing argument 1 of ‘show’ from incompatible pointer type [-Wincompatible-pointer-types]
show(SCLList);
^~~~~~~
list_dir_files_4.c:105:6: note: expected ‘Node * {aka struct nlist *}’ but argument is of type ‘struct Node *’
void show(Node *Head)
^~~~
list_dir_files_4.c: At top level:
list_dir_files_4.c:159:14: error: conflicting types for ‘listFilesRecursively’
struct Node *listFilesRecursively(char *basePath, char *suffix)
^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:138:15: note: previous implicit declaration of ‘listFilesRecursively’ was here
B02List = listFilesRecursively(path, suffix);
^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c: In function ‘listFilesRecursively’:
list_dir_files_4.c:168:9: warning: ‘return’ with no value, in function returning non-void
return;
^~~~~~
list_dir_files_4.c:159:14: note: declared here
struct Node *listFilesRecursively(char *basePath, char *suffix)
^~~~~~~~~~~~~~~~~~~~
list_dir_files_4.c:185:12: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
return Head;

有人能帮我吗?我一直在尝试处理类型声明,除了将函数***listFilesRecursive***声明为void只是为了在其中调用show(Head(,而不在main((中返回我需要的链表之外,似乎什么都不起作用。

声明

void listFilesRecursively(char *path, char *suffix);

表示函数不返回任何东西,并且确实与的定义(实现(相冲突

struct Node *listFilesRecursively(char *basePath, char *suffix)

声明和定义必须相同。

相关内容

  • 没有找到相关文章

最新更新