【问题标题】:How do I remove the "incompatible pointer type" warning from my code?如何从我的代码中删除“不兼容的指针类型”警告?
【发布时间】:2020-12-08 15:53:44
【问题描述】:

此代码读取输入文本文件,并根据其内容创建输出文件。

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

它会生成正确的输出文件,但是每当我编译它时就会出现这个错误。

B:\CodeBlocks\Projects\Programming in C\hw_4\Homework_4\main.c|44|警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“strncat”的参数 2|

警告似乎来自 strncat 的唯一行。有谁知道如何解决这个问题?

【问题讨论】:

  • 不要尝试将字符(或int whotsoever)连接到字符串。 strncat 使用有效的、以 null 结尾的字符串作为两个操作数。
  • 你可以尝试根据strncat的定义强制转换参数2 "char * strncat (char * destination, const char * source, size_t num );"第 44 行变为 strncat(word, (const char *) &c, 1);这只会删除警告,如果有任何问题,也不会解决问题。
  • int isAlphanumeric()? Just use isalnum().

标签: c char c-strings


【解决方案1】:

变量 c 被声明为具有 int 类型。

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

所以这个调用中使用的表达式&amp;c

strncat(word, &c, 1);

具有int * 类型而不是char * 类型。

为一个字符调用strncat 是没有意义的。此外,数组字具有不确定的值,因为它没有被初始化。

char word[30], longest_word[30];

你可以写

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

然后像下面这样

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

也就是说,变量n将跟踪存储在数组word中的字符串的当前长度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多