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

时间:2016-05-04 16:26:42

标签: c

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

  

完整的程序列在后面。

我和老师谈过,她标记了

   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上查看了类似的问题,发现我必须在fprintf之前放置filepointer。 我的第二块坏代码现在看起来像这样:

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);

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

我的完整计划:

/*
* 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);
    */

2 个答案:

答案 0 :(得分:1)

如果要将更多或其他参数传递给函数,则必须同时修改调用和函数定义,以使它们彼此兼容。从

改变它
void print_rmchr (char str[], char ch);

为:

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

按原样,符号filePtrmain的本地符号,print_rmchr中未知。

答案 1 :(得分:0)

应用所有注释后,rmchr()函数变为:

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

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

   str[j] = '\0';
}

打开文件时,此模式会给您带来问题,主要是因为以下代码的完全缩进:

FILE *filePtr;

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

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

else


suggest using the following pattern:

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

if( NULL == (filePtr = fopen("rmchr.out", "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