[英]Getting errors when creating multi file program in c
我正在嘗試用 C 語言創建一個大型的多文件程序,但出現錯誤。
該程序實際上是一個智能電話簿,可以執行多項任務,例如列出、添加、搜索和刪除姓名。
對於這些任務中的每一個,我都創建了一個文件,其中包含執行任務所需的函數和變量。
這是我完成編碼的文件:
(1) us-names.txt
(存儲名稱的數據庫):
Mohammed
Ali
Harry
Peter
Hussain
Mark
John
Yu
Mohammed
Christina
Mohammed
Fatima
(2) Request.c
文件(接收用戶的請求並執行需要的任務):
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
//node declaration:
typedef struct node
{
char name[20];
struct node* link;
}
node;
void import_names(void) ;
void add_name(string name) ;
void search_for(string name) ;
void delete_name(string name) ;
int main(int argc , string argv[])
{
//create the hashtable and import the names:
import_names() ;
//check if there is any mistake in the command:
if(argc != 3 || argc != 4)
{
printf("Could not recognize the command, please enter one of the three commands: \n ./phone add name \n ./phone search-for name \n ./phone delete name \n") ;
return 1 ;
}
//otherwise, preform the command:
if(strcmp(argv[1] , "add") == 0)
{
add_name(argv[2]) ;
return 0 ;
}
if(strcmp(argv[1] , "search-for") == 0)
{
search_for(argv[2]) ;
return 0 ;
}
if(strcmp(argv[1] , "delete") == 0)
{
delete_name(argv[2]) ;
return 0 ;
}
else
{
printf("Could not recognize the command, please enter one of the three commands: \n ./phone add name \n ./phone search-for name \n ./phone delete name \n") ;
return 1 ;
}
}
3- Import.c 文件(從文本文件中導入名稱並將它們散列並將它們存儲在散列表中):
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
//node declaration:
typedef struct node
{
char name[20];
struct node* link;
}
node;
unsigned int hash(char* buffer , unsigned int CAPACITY) ;
void import_names(void)
{
//declarations:
FILE* fp = fopen("us-names.txt" , "a+") ;
if(fp == NULL)
{
printf("CANNOT OPEN THE FILE !\n") ;
fclose(fp) ;
exit ;
}
char buffer[20] ;
unsigned int key = 0;
unsigned int CAPACITY = 100 ;
struct node* temp = NULL ;
struct node* hashtable[CAPACITY] ;
for(int i = 0 ; i < CAPACITY ;i++)
{
hashtable[i] = NULL ;
}
struct node* curr = NULL ;
int num_of_names = 0;
//use a loop to iterate over the FILE:
for(int i = 0 ; !feof(fp) ; i++)
{
//Import name from the FILE & store into buffer:
fgets(buffer , 20 , fp) ;
//pass the name into the hash_function and get the key:
key = hash(buffer , CAPACITY) ;
//put the name in a node then in it's right place in the hashtable using the key:
temp = malloc(sizeof(struct node)) ;
sprintf(temp->name , "%s" , buffer) ;
temp->link = NULL ;
//if the key place of the hashtable is already populated, preform the collision protocol!:
if(hashtable[key] != NULL)
{
for(curr = hashtable[key] ; curr->link != NULL ; curr = curr->link)
{
}
curr->link = temp ;
}
//else if it's unpopulated, just store the name in this place:
else
{
hashtable[key] = temp ;
}
num_of_names++ ;
}
//close the FILE :
fclose(fp) ;
}
4- Hash.c 文件(一個包含散列函數的小文件):
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
unsigned int hash(char* buffer , unsigned int CAPACITY)
{
unsigned int sum = 0 ;
for(int i = 0; buffer[i] != '\0' ;i++)
{
sum += buffer[i] ;
}
return sum % CAPACITY ;
}
5- Search.c 文件(包含一種用於搜索任何名稱的二進制搜索功能):
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
#include "phonehead.h"
//node declaration:
typedef struct node
{
char name[20];
struct node* link;
}
node;
unsigned int CAPACITY = 100 ;
struct node* hashtable[CAPACITY] ;
unsigned int hash(char* buffer , unsigned int CAPACITY) ;
void search_for(string name)
{
//hash the name to get the place in which the wanted name is stored in the hash function:
unsigned int place = hash(name,CAPACITY);
//go to the location in the hashtable in which the name is stored, and check if the name is in the first node of the chain.
if(strcmp(hashtable[place]->name , name) == 0)
{
printf("FOUND\n") ;
}
//else, go to the node pointed by the current node and check if it contain the wanted name, and repeat this step until reaching null.
else if(strcmp(hashtable[place]->name , name) != 0)
{
struct node* curr = NULL ;
for(curr = hashtable[place]->link ; curr != NULL ; curr = curr->link)
{
if(strcmp(curr->name , name) == 0)
{
printf("FOUND\n");
exit ;
}
}
printf("NOT FOUND!\n") ;
}
}
除了頭文件:
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include <stdint.h>
#include <string.h>
typedef struct node
{
char name[20];
struct node* link;
}
node;
unsigned int hash(char* buffer , unsigned int CAPACITY) ;
unsigned int CAPACITY = 100 ;
struct node* hashtable[CAPACITY] ;
void import_names(void) ;
void add_name(string name) ;
void search_for(string name) ;
void delete_name(string name) ;
但是當嘗試使用命令鏈接/編譯文件時
gcc phonehead.h requist.c Import.c Hash.c Search.c -o phone
我收到這些錯誤:
phonehead.h:15:14: error: variably modified ‘hashtable’ at file scope
15 | struct node* hashtable[CAPACITY] ;
| ^~~~~~~~~
In file included from Search.c:6:
phonehead.h:15:14: error: variably modified ‘hashtable’ at file scope
15 | struct node* hashtable[CAPACITY] ;
| ^~~~~~~~~
Search.c:8:16: error: redefinition of ‘struct node’
8 | typedef struct node
| ^~~
本聲明
struct node* hashtable[CAPACITY] ;
在文件范圍內聲明一個可變長度數組。 您不能在文件范圍內聲明可變長度數組。 在文件作用域中聲明的對象具有靜態存儲持續時間,但可變長度數組可能只有自動存儲持續時間,即它們被允許在塊作用域中定義。
而不是聲明用作數組大小的變量CAPACITY
導致聲明可變長度數組
unsigned int CAPACITY = 100 ;
struct node* hashtable[CAPACITY] ;
你可以使用像這樣的定義指令
#define CAPACITY 100
struct node* hashtable[CAPACITY] ;
或像這樣的數字常數
enum { CAPACITY = 100 };
struct node* hashtable[CAPACITY] ;
此外,您在文件范圍內兩次聲明了這樣的數組:在頭文件phonehead.h
和包含頭文件的文件Search.c
中。 兩次聲明數組是沒有意義的。 你應該只聲明一次。 由於文件范圍內的數組未顯式初始化,因此此類聲明引入了暫定定義。
你兩次定義了結構節點
typedef struct node
{
char name[20];
struct node* link;
}
node;
在標題phonehead.h
和包含標題的模塊中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.