简体   繁体   中英

Iterating through pointer character array

The function readFile(filename) reads the contents of the file and stores it in heap using malloc, it returns char* datatype. I want to store the content in an array in main so that I can count the words. How do I do so? This is the code:

     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;
    }

When you call "Countwords", your arguments cant be seen by the main as they arent defined there. Even if you use them in your other funtion - this is a different scope. Maybe you could return a char*[] holding these paramaters you want to return. Or better yet, define all of those arguments in main, have your first function pass them "by reference". This way you have the values put in a place where you can access them later for your "Counwords"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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