繁体   English   中英

在c中创建多文件程序时出错

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM