c - 从不兼容的指针类型传递参数 (C)

标签 c

我需要创建一个程序,将硬编码输入列出到不同的文件中。我在 fprintf 部分遇到问题。

The full program is listed in the back.

我已经和我的老师谈过了,她标记了

   else
    {
        print_rmchr(str1, chr1);
        print_rmchr(str2, chr2);
        print_rmchr(str3, chr3);
        print_rmchr(str4, chr4);
        print_rmchr(str5, chr5);

        if (fclose(filePtr) != 0)
            printf("Unable to close the data file.\n");
    }

if (fprintf("The string '%s' will be removed from '%c' characters. \n\n", str, ch) < 0)
    printf("Unable to print non-modified string with a modifying character. \n");

rmchr(str, ch);

if (fprintf("New modified string is: '%s'. \n\n", str) < 0)
    printf("Unable to print new modified string. \n");

作为我作业中代码的错误部分。

我在starkoverflow上查看了类似的问题,发现我必须将filepointer放在fprintf之前。 我的第二 block 错误代码现在看起来像这样:

if (fprintf(filePtr, "The string '%s' will be removed from '%c' characters. \n\n", str, ch) < 0)
    printf("Unable to print non-modified string with a modifying character. \n");

rmchr(str, ch);

if (fprintf(filePtr, "New modified string is: '%s'. \n\n", str) < 0)
    printf("Unable to print new modified string. \n");

然后编译器对我大喊大叫,因为我不知道为什么会有文件指针,所以我也将其修复为

print_rmchr(filePtr, str, chr);

但随后它开始对我大喊大叫,好像“从不兼容的指针类型传递参数”。它看起来适合我,但我想缺少一些东西(?)。

我的完整计划:

/*
* A simple program to remove certain characters from the given strings                              
*/

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

int main () {

    // print array before and after editing array
    void print_rmchr (char str[], char ch); 

    char str1[20] = "abracadabra";      //string #1
    char str2[20] = "abracadabra";      //string #2
    char str3[20] = "abracadabra";      //string #3
    char str4[20] = "aaaa";             //string #4
    char str5[20] = "aaaa";             //string #5

    char chr1 = 'a';                    //character #1
    char chr2 = 'b';                    //character #2
    char chr3 = 'n';                    //character #3
    char chr4 = 'a';                    //character #4
    char chr5 = 'n';                    //character #5

    FILE *filePtr;

    filePtr = fopen("rmchr.out", "w");

    if( filePtr == NULL)
      printf("Unable to open the data file.\n");

   else
    {
        print_rmchr(filePtr, str1, chr1);
        print_rmchr(filePtr, str2, chr2);
        print_rmchr(filePtr, str3, chr3);
        print_rmchr(filePtr, str4, chr4);
        print_rmchr(filePtr, str5, chr5);

        if (fclose(filePtr) != 0)
            printf("Unable to close the data file.\n");
    }
    return 0;
}

//remove certain characters from array
void rmchr(char str[], char ch) {
   int i, j = 0;    //loop variable
   int size;        //lengh 
   char new_str[20];    //new array

   size = strlen(str);

   for (i = 0; i < size; i++) {
      if (str[i] != ch) {
         new_str[j] = str[i];
         j++;
      }
   }
   new_str[j] = '\0';

   strcpy(str, new_str);
}

// print array before and after editing array
void print_rmchr (char str[], char ch){

    //remove certain characters from array
    void rmchr(char str[], char ch);

    if (fprintf(filePtr, "The string '%s' will be removed from '%c' characters. \n\n", str, ch) < 0)
        printf("Unable to print non-modified string with a modifying character. \n");

    rmchr(str, ch);

    if (fprintf(filePtr, "New modified string is: '%s'. \n\n", str) < 0)
        printf("Unable to print new modified string. \n");
}

    /* In case you will need user input:
    //USER INPUT
    printf("Enter the string : \n");
    gets(str);

    printf("Enter character which you want to delete : \n");
    scanf("%ch", &ch);

    print_rmchr(str, ch);
    */

最终更正版本:

/*
* A simple program to remove certain characters from the given strings                              
*/

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

#define MAX 20

int main () {

    // print array before and after editing array
    void print_rmchr (FILE *filePtr, char str[], char ch);

    char str1[MAX] = "abracadabra";      //string #1
    char str2[MAX] = "abracadabra";      //string #2
    char str3[MAX] = "abracadabra";      //string #3
    char str4[MAX] = "aaaa";             //string #4
    char str5[MAX] = "aaaa";             //string #5

    char chr1 = 'a';                    //character #1
    char chr2 = 'b';                    //character #2
    char chr3 = 'n';                    //character #3
    char chr4 = 'a';                    //character #4
    char chr5 = 'n';                    //character #5

FILE *filePtr = NULL; // best to always initialize stack variables

if( NULL == (filePtr = fopen("rmchr.txt", "w") ) )
{
    // output your message, plus the system error message
    perror("fopen rmchr.out for write failed");

    // exit() and EXIT_FAILURE in 'stdlib.h'
    exit(EXIT_FAILURE);  
}
// implied else, fopen successful

    print_rmchr(filePtr, str1, chr1);
    print_rmchr(filePtr, str2, chr2);
    print_rmchr(filePtr, str3, chr3);
    print_rmchr(filePtr, str4, chr4);
    print_rmchr(filePtr, str5, chr5);

    if (fclose(filePtr) != 0)
        printf("Unable to close the data file.\n");

    return 0;
}

//remove certain characters from array
void rmchr(char str[], char ch)
{
    size_t i;       //loop variable
    int j = 0;      //loop variable
    char new_str[MAX];    //new array

    for (i = 0; str[i]; i++) {
      if (str[i] != ch) {
         new_str[j] = str[i];
         j++;
      }
    }
    new_str[j] = '\0';

    strcpy(str, new_str);  
}

// print array before and after editing array
print_rmchr (FILE *filePtr, char str[], char ch){

    //remove certain characters from array
    void rmchr(char str[], char ch);

    if (fprintf(filePtr, "All instances of character %c will be removed from string '%s. \n\n", ch, str) < 0)
        printf("Unable to print non-modified string with a modifying character. \n");

    rmchr(str, ch);

    if (fprintf(filePtr, "New modified string is: '%s'. \n\n", str) < 0)
        printf("Unable to print new modified string. \n");
}

    /* In case you will need user input:
    //USER INPUT
    printf("Enter the string : \n");
    fgets(str);

    printf("Enter character which you want to delete : \n");
    scanf("%ch", &ch);
    //need to check the returned value of scanf

    print_rmchr(str, ch);
    */

最佳答案

当您想要向函数传递更多或其他参数时,必须修改调用和函数定义,以便它们彼此兼容。更改自

void print_rmchr (char str[], char ch);

至:

void print_rmchr (FILE *filePtr, char str[], char ch);

事实上,符号 filePtrmain 的本地符号,在 print_rmchr 中是未知的。

关于c - 从不兼容的指针类型传递参数 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37033236/

相关文章:

c - 最小公因数

c - 将并行进程的结果添加到共享内存

c - 在 C 中重新分配字符串值

c - 如何让 Cilk 与 Cygwin 一起工作?

c - 如何添加两个字符串?

c - Waitpid 的行为就像处于非阻塞模式

c++ - 如何通过地址的数值来判断地址合法或非法?

c - C中合并两个数组

C:指针题

c - 为什么这些构造使用增量前和增量后未定义的行为?