繁体   English   中英

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

[英]Passing argument from incompatible pointer type (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上研究了类似的问题,发现我必须将filepointer放在fprintf之前。 我的第二部分坏代码现在看起来像这样:

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

filePtr ,符号filePtrmain是本地的,在print_rmchrprint_rmchr

应用所有注释后, 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM