Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 遍历指针字符数组_C - Fatal编程技术网

C 遍历指针字符数组

C 遍历指针字符数组,c,C,函数readFile(filename)读取文件的内容并使用malloc将其存储在堆中,它返回char*数据类型。我想将内容存储在main中的一个数组中,以便我可以计算单词数。我该怎么做?代码如下: int main() { char *char1; char1 = readFile("test1.txt"); printf("%s", char1[0]); //This does not

函数readFile(filename)读取文件的内容并使用malloc将其存储在堆中,它返回char*数据类型。我想将内容存储在main中的一个数组中,以便我可以计算单词数。我该怎么做?代码如下:

     int main()
    {

    char *char1;

    char1 = readFile("test1.txt");
    printf("%s", char1[0]);           //This does not print anything

    CountWords(*char1, &Alpha, &SentChk, &punct, &Words, &totalSents, &onlyVowel_e);  

    /*function header of CountWords: void CountWords(char ch, int *Alpha, int *SentChk, int *punct, 
    int *Words, int *totalSents, int *onlyVowel_e);*/

    printf("%d", Words);
    printf("%d", totalSents);

    return 0;
    }

当你调用“Countwords”时,你的论点不能被主体看到,因为它们在那里没有定义。即使你在你的其他职能中使用它们-这是一个不同的范围。也许您可以返回一个char*[],其中包含要返回的参数。或者更好的是,在main中定义所有这些参数,让您的第一个函数“通过引用”传递它们。这样,您就可以将这些值放在一个地方,以便您以后可以访问这些值,以获取CountWords的函数头,但您是否有函数
CountWords
文档?每个参数的含义是什么?如果
char1
是指向字符的指针,则
char1[0]
是单个字符,而不是字符串。请刷新教科书中有关指针和数组的章节。或者甚至可能是“hello world”程序的前几个部分,以及如何打印字符串。