0
votes

I'm getting a bunch of pointer errors and pointers are not exactly my strongest point. Most of my errors have to do with passing an argument.

The errors are:

HW5_mkhan44.c: In function ‘main’: HW5_mkhan44.c:31:3: warning: passing argument 1 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘struct FILE *’ but argument is of type ‘struct FILE **’

HW5_mkhan44.c:31:3: warning: passing argument 2 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘const char *’ but argument is of type ‘char * (*)[100]’

HW5_mkhan44.c:31:3: warning: passing argument 3 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘int *’ but argument is of type ‘int **’

HW5_mkhan44.c:31:3: warning: passing argument 4 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘int *’ but argument is of type ‘int **’

This is the code:

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

int word_search(const char *word, const char *str);
int character_count(const char *curr_str);
void search_and_count(FILE *fpter, const char *c, int *len, int *result);

int main(void)
{
        FILE *fileptr;
        char filename[100];
        char* word[100];
        int *length;
        int *num;

        printf("Please input the text file name: ");
        scanf("%s", filename);

        fileptr = fopen(filename, "r");

        if(fileptr == NULL)
                printf("File does not exist\n");
        else{
                printf("Please enter a word to search: ");
                scanf("%s", word);
                search_and_count(&fileptr, &word, &length, &num);

                if(*num == 0){
                        printf("Word exists in file");
                        printf("There are %d characters in file", *length);
                }
                else
                        printf("Word does not exist in file");
        }
        fclose(fileptr);

        return 0;
}

int word_search(const char *word,const char *str)
{
        int num;
   if(strcmp(word, str) == 0){
                num = 0;
                return(num);
        }
        else{
                num = 1;
                return(num);
        }
}

int character_count(const char *current_str)
{
        int word_length;

        word_length = strlen(current_str);

        return(word_length);
}

void search_and_count(FILE *fpter, const char *c, int *len, int *result)
{
        char line[120];

        while(fgets(line, sizeof(line), fpter)){
                char* t = strtok(line, " ");
                *len = word_search(c, t);
                *result = character_count(t);
        }
}
2

2 Answers

6
votes

This is what looks like syntax errors:

  • In search_and_count(&fileptr, &word, &length, &num); you already declared them as pointers, so remove all &s if you want to send the pointer
  • char* [] declaration should be just char [] because it can be treated as the pointer

Then step-over every statement to see if you have any logical errors and good luck!

1
votes

Problem 1: word seems to be the buffer to store the signe word you search. But you define it as an array of 100 pointers. Try:

   char word[100];  // without *.  Nom it can be used as a c-string. 

Problem 2: the parameters you give when you invoke your search function are incorrect. Try:

    search_and_count(fileptr, word, &length, &num);

Problem 3: the variable length and num are defined as pointers to int. But you really want to pass the address of an int. So change their definition:

  int length;
  int num;

General information:

If you want to pass a pointer to a variable of type T as parameter to a function, there are two possibilities and a variant:

  • either you already have a valid pointer T *p; that points already to somewhere. Then you can pass directly this pointer p as argument. In your code, it's the case of the fileptr which value is set to point to a valid FILE object by fopen()
  • or you already have an object T o; and you would like to pass the address of it a parameter to the function (so that the function could change the content of this object. You'd then pass &p. That's the case of your int variables.
  • or you have already an array T a[100]; and you'd like to pass a pointer to the array. Then, you'd call your function passing a because the name of the array, will then be understood as a pointer to the first element of the array.