【问题标题】:C passing and pointersC 传递和指针
【发布时间】:2013-04-03 02:29:03
【问题描述】:

所以我正在编写这个程序,我不知道如何通过引用或值传递。我只需要将值从一个函数(processFile)传递给另一个函数(printReport)。任何有关如何执行此操作的帮助将不胜感激。

#include <stdio.h>
#include <stdlib.h>

void printInstructions();
int processFile(FILE *fileIn);
void printReport();

int main(void)
{
   FILE *fileIn;
   int words = 0,
        lines = 0,
        characters = 0,
        sentences = 0;

   printInstructions();

   fileIn = fopen("input09.txt", "r");

   if(fileIn == NULL)
      printf("\n\nERROR\n\n");

   processFile(fileIn);

   printReport(words, lines, characters, sentences);

}

void printInstructions()
{
   printf("\n  Program reads a file and returns the number of  \n");
   printf("lines, words, characters, and sentences in the file.\n\n");

   return;
}

int processFile(FILE *fileIn)
{
        int sentences = 0,
             words = 0,
             lines = 0,
             characters = 0,
             ch;

   while(ch != EOF)
   {
            ch = fgetc(fileIn);

            if(ch == '\n' || ch == 60)
                    lines++;

            if(ch == '.')
                    sentences++;

            if(ch != '.' || ch != ' ' || ch != '\n')
                    characters++;

            if(ch == ' ')
                    words++;
   }

   return 0;
}

void printReport()
{
   printf("This file contains NUMBER lines.\n");
   printf("This file contains NUMBER words.\n");
   printf("This file contains NUMBER    characters.\n");
   printf("This file contains NUMBER sentences.\n\n");

   return;
}

【问题讨论】:

    标签: c function pointers pass-by-reference pass-by-value


    【解决方案1】:

    尝试改成这样:

    int processFile(FILE *fileIn, int *sentences, int *words, int *lines, int *characters)
    {
        int ch;
        ...
    
            if(ch == '\n' || ch == 60)
                    *lines++;
        ...
    
    int main(void)
    {
        ...
        processFile(fileIn, &sentences, &words, &lines, &characters);
        ...
    

    因此,在此更改中,函数processFile 现在按指针接收四个参数。当您访问这些参数时,使用(例如)*lines 来取消引用指针。

    main() 中,当您调用processFile() 时,会将地址传递给您的参数。

    希望这会有所帮助;我认为您想了解 C/C++ 中的参数传递(按值传递、按指针传递和按引用传递 - 您可能想在 Google 上了解这些术语)。

    【讨论】:

      【解决方案2】:

      如果您想在函数中从 FILE 中读取数据,那么您已经做到了。

      FILE *fileIn 指向堆栈上的某个内存块。基本上它是一个数字。当您将该“数字”传递给另一个函数时,您仍然指向同一事物,因此您可以编辑您所指向的内容。

      但是,如果您使 fileIn 指向您函数中的其他内容。例如:fileIn = null; 您必须将 fileIn 作为双指针传递。它必须有 2 个 *,如下所示:void foo(FILE **bar)

      【讨论】:

        【解决方案3】:

        你可以从定义一个数据结构开始,将给定句子的所有统计数据保存为

        typedef struct stats {
            int words;
            int lines;
            int characters;
            int sentences;
        }stats;
        

        在 main 中,您可以将这个数据结构的对象创建为

        struct stats stats_obj;
        

        要从processFile 中检索统计信息,您可以将对此对象的引用传递为

        processFile(fileIn, &stats_obj);
        

        processFile 中,在处理结束时,您可以将结果存储为

        stats_obj->words = words;
        stats_obj->lines = lines;
        .........
        

        processFile 返回后,您可以将相同的内容传递给 printReport,就像 printReport(stats_obj) 一样,并在此函数中将其打印为

        void printReport(struct stats stats_obj)
        {
            printf("This file contains %d lines.\n", stats_obj.lines);
            printf("This file contains %d words.\n" , stats_obj.words);
            printf("This file contains %d characters.\n", stats_obj.characters);
            printf("This file contains %d sentences.\n\n", stats_obj.sentences);
        
            return;
        }
        

        注意:始终建议检查fopen 的输出,因此您必须在fileIn = fopen("input09.txt", "r"); 之后插入NULL 检查fileIn

        【讨论】:

          【解决方案4】:

          你可以这样做:

          int main(void)
          {
            ...
            int sentences = 0,
                   words = 0,
                   lines = 0,
                   characters = 0,
                   ch;
          
            processFile(fileIn, &words, &lines, &characters, &sentences);
          
            printReport(words, lines, characters, sentences);
          }
          
          int processFile(FILE *fileIn, int *pWords, int *pLines, 
                                        int *pLines, int *pSentences)
          {
                 ...
                 *pLines++;
                 *pSentences++;
                 *pCharacters++;
                 *pWords++;
          }
          

          【讨论】:

            猜你喜欢
            • 2015-02-11
            • 2021-11-12
            • 1970-01-01
            • 1970-01-01
            • 2020-08-16
            • 1970-01-01
            • 2013-01-13
            • 2012-03-15
            • 2021-07-09
            相关资源
            最近更新 更多