user5737301
user5737301

Reputation:

Passing argument from incompatible pointer type (C)

I need to create a program that lists the hard-coded input into a different file. I have encountered issue in the fprintf section.

The full program is listed in the back.

I have talked to my teacher and she marked

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

and

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

as wrong part of the code in my assignment.

I looked at similar questions on starkoverflow and found that I have to put filepointer before fprintf. My second chunk of the bad code now looks like this:

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

And then compiler yelled at me for not knowing why there was file pointer, so I also fixed it as

print_rmchr(filePtr, str, chr);

But then it began to yell at me as if "passing argument from incompatible pointer type". It looks right for me, but something is missing(?) I guess.

My full program:

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

FINAL, corrected version:

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

Upvotes: 0

Views: 1001

Answers (2)

user3629249
user3629249

Reputation: 16540

after applying all the comments, the rmchr() function becomes:

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

When opening a file, this pattern will give you problems, mostly due to the extranious indenting of any following code:

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

Upvotes: 0

M Oehm
M Oehm

Reputation: 29126

When you want to pass more or other arguments to a function, you must modify both the calls and the function definition, so that they are compatible with each other. Change it from

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

to:

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

As is, the symbol filePtr is local to main and not known in print_rmchr.

Upvotes: 1

Related Questions