【问题标题】:C - Error with function parameters when it is being calledC - 调用函数参数时出错
【发布时间】:2016-02-01 19:58:26
【问题描述】:

当我试图调用一个函数时,到目前为止我尝试过的参数只是导致终端说“函数的参数太少”,而我可以说它想要写的参数来自当它被宣布。我已经阅读了一些关于此的不同文档以及调用参数是什么以及按值或按引用调用之间的区别,但我仍然无法找出问题所在。 下面是代码的主要部分,其中包含调用函数以及一些变量。

int main(int argc, char *argv[])
{//main()

char *listwords;

processfile(listwords); //<- this is the line that is causing the problem

WordList mylist;

initialiselist(&mylist);

addword(&mylist, createfillednode(listwords));

printlist(&mylist);
}//main()

下面是processfile()函数:

//process the file
void processfile(WordList *wordList, int argc, char *argv[])
{//process file
    //file pointer
    FILE *f;
    //open the file
    f = fopen(argv[1], "r");
    //check it opened correctly
    if(f == NULL)
    {//if statement
        printf("cannot read file\n");
    }//if statement

    fseek(f, 0, SEEK_END);

    //declare variables
    char *listwords;
    long size = ftell(f);
    char *token;

    //seek beginning of file
    fseek(f, 0, SEEK_SET);
    //set the size of array to the file size
    listwords = (char*)malloc(size+1);
    listwords[size] = '\0';
    //reads the data from the file
    fread(listwords, size, 1, f);

    int i;
    for(i=0; (token = strsep(&listwords, " ")); i++)
    {//for loop replace certain characters with spaces
        if (token != ".")
        {
            //pointer from the token to the dictionary
            wordList->token;
        }else if (wordList->token != "!")
        {

            wordList->token;
        }else if (token != "?")
        {

        wordList->token;
        }else if (token != "\"")
        {

            wordList->token;
        }else if (token != ","){

            wordList->token;
        }else
        {

            wordList->token;
        }
        //increment token to the next word
        token++;
    }//for loop replace certain characters with spaces
    fclose(f);
    return;
}//process file

谢谢。

【问题讨论】:

  • processfile 采用 3 个参数。您正在通过 1。可能有什么问题?
  • too.few.arguments.to.function。怎么不清楚?
  • processfile(listwords); --> processfile(listwords, argc, argv);,提供,WordListchar 的 typedef。

标签: c


【解决方案1】:

您已声明 processfile 接受三个参数。

void processfile(WordList *wordList, int argc, char *argv[])

但你只给它一个。

processfile(listwords);

而且它也是错误的类型。它应该是 WordList,但它是一个字符串 (char *)。

char *listwords;

在 C 语言中,您必须为函数提供正确数量的参数和正确的类型(C can do some type casting,但它的定义很严格,通常与数字有关)。

有一个例外。 Variadic arguments 让您传递未定义数量的参数。这就是像printf 这样的函数的工作方式。一般来说,您应该尽可能避免使用可变参数函数,它们会增加复杂性并破坏类型检查。


在您的情况下,processfile 只需要两个参数:单词列表和要打开的文件名。 argc 从未使用过,并且知道文件名来自 argv[1] 会对函数施加不必要的限制。

void processfile(WordList *wordList, char *filename)

然后就可以调用...

WordList *wordList = ...generate the wordlist somehow...

processfile(wordList, argv[1]);

【讨论】:

    【解决方案2】:

    processfile() 函数根据您的定义采用三个参数:

    void processfile(WordList *wordList, int argc, char *argv[])

    但是,在您的 main() 函数中,您只传递了一个参数:

    processfile(listwords)

    要使这项工作如您所愿,您必须传递所有三个参数;你的listwords,以及参数的计数器和向量:

    processfile(listwords, argc, argv)

    但是,总的来说,据我所知,从编程上讲,这通常不是一个好主意。一个函数通常应该接受某种专门的输入并返回一个相关的值,而不是来自命令行的输入——应该在实际调用的函数之前进行解析。查看getoptargparse 以正确处理参数,确定您真正想要传递给processfile() 的参数并仅传递与您所在的文件相关的参数试图处理。通过阅读您的代码,可能只是文件描述符,经过一些参数解析错误更正后,应该在main() 中打开:

    void processfile(WordList *wordList, FILE *f)
    {
        fseek(f, 0, SEEK_END);
    
        //declare variables
        char *listwords;
        long size = ftell(f);
        char *token;
    
        fseek(f, 0, SEEK_SET);
        listwords = (char*)malloc(size+1);
        listwords[size] = '\0';
        fread(listwords, size, 1, f);
        - - - - - - - - - 8< - - - - - - - - - 
    

    这样,您可以在别处打开文件,并在任何上下文中调用的任何打开文件上重复使用processfile(),使其成为更强大的函数。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    相关资源
    最近更新 更多