DuckTooth
DuckTooth

Reputation: 23

How do I remove the "incompatible pointer type" warning from my code?

This code reads an input text file, and creates an output file based on its contents.

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

#define OUT 0
#define IN  1
#define MAX 28
#define BLOCK 4000

/* Check whether the character is alphanumeric */
int isAlphanumeric(char c) {
    return ('a' <= c && c <= 'z') ||
      ('A' <= c && c <= 'Z') ||
      ('0' <= c && c <= '9');
}

int main(int argc, char *argv[]) {
    int c, state = OUT, length = 0, i, j, counter[MAX];
    char word[30], longest_word[30];
    FILE *input, *output;   /* FILE pointers to open the file */

    /* Initialize the counter */
    for (i = state; i < MAX; i++)
        counter[i] = 0;

    /* Open the file */
    input = fopen("complete_shakespeare.txt", "r");
    output = fopen("word_length_histogram.txt", "w");

    /* Keep reading the character in the file */
    while ((c = getc(input)) != EOF) {
        /* If the character is alphanumeric, record it */
        if (isAlphanumeric(c)) {
            strncat(word, &c, 1);
        }
        /* If the character is not alphanumeric, increment the corresponding counter, and additionally, record longest word. */
        else {
            length = strlen(word);
            if (length == 27) strcpy(longest_word, word);
            counter[length] += 1;
            memset(word, 0, sizeof(word));
        }
    }
    /* If the file ends with a word, record its length */
    if (isAlphanumeric(word[0])){
        length = strlen(word);
        counter[length] += 1;
    }

    /* print the longest word to the file */
    fprintf(output, "%s\n\n", longest_word);

    /* Make the histogram */
    for (i = 1; i < MAX; i++) {
        int dividend = counter[i] / 4000 + 1;
        fprintf(output, "%2d %6d ", i, counter[i]);
        for (j = dividend; j >= 1; j--){
            if (counter[i] != 0)
                fprintf(output, "*");
        }
        fprintf(output, "\n");
    }

    /* Don't forget to close the FILEs */
    fclose(input);
    fclose(output);

    return 0;
}

It produces the correct output file, but this error comes up whenever I compile it.

B:\CodeBlocks\Projects\Programming in C\hw_4\Homework_4\main.c|44|warning: passing argument 2 of 'strncat' from incompatible pointer type [-Wincompatible-pointer-types]|

The warning seems to come from the only line with strncat. Does anyone know how this can be remedied?

Upvotes: 0

Views: 908

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311038

The variable c is declared as having the type int.

int c, state = OUT, length = 0, i, j, counter[MAX];
^^^^^^

So the expression &c used in this call

strncat(word, &c, 1);

has the type int * instead of the type char *.

There is no sense to call strncat for one character. Moreover the array word has indeterminate values because it was not initialized.

char word[30], longest_word[30];

You could write

char word[30], longest_word[30];
word[0] = '\0';

And then something like the following

   size_t n = 0;
   while ((c = getc(input)) != EOF) {
    /* If the character is alphanumeric, record it */
    if (isAlphanumeric(c)) {
        word[n] = ( char )c;
        word[++n] = '\0';
    }
    /* If the character is not alphanumeric, increment the corresponding counter, and additionally, record longest word. */
    else {
        if (n == 27) strcpy(longest_word, word);
        counter[n] += 1;
        n = 0;
        word[n] = '\0';
    }
}

That is, the variable n will keep track of the current length of the string stored in the array word.

Upvotes: 1

Related Questions