Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.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_Char_C Strings - Fatal编程技术网

C 我如何删除“";不兼容的指针类型“;我的代码中的警告?

C 我如何删除“";不兼容的指针类型“;我的代码中的警告?,c,char,c-strings,C,Char,C Strings,此代码读取输入文本文件,并根据其内容创建输出文件 #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) { r

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

#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;
}
#包括
#包括
#包括
#定义出0
#在1中定义
#定义最大值28
#定义块4000
/*检查字符是否为字母数字*/
int isAlphanumeric(字符c){

return('a'变量c被声明为具有类型
int

int c, state = OUT, length = 0, i, j, counter[MAX];
^^^^^^
因此,此调用中使用了表达式
&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

中的字符串的当前长度,不要尝试连接字符(或
int
任何人)到字符串。
strncat
使用有效的、以null结尾的字符串作为两个操作数。您可以尝试根据strncat的定义强制转换参数2“char*strncat(char*destination,const char*source,size_t num);”第44行变为strncat(word,(const char*)&c,1);这只会删除警告,如果存在任何问题,则不会解决问题。
int isAlphanumeric()