0

error coming-error: invalid conversion from 'char*' to 'char' [-fpermissive]

cout<< par(s,n1,num1,word); 

Why calling par() function in main causing invalid conversion from char* to char?

Tried a lot to find the reason myself. I am a beginner in c++. I don't find anything wrong in the par() function. I haven't done assignment mismatch anywhere in the function, well thinking that there may be an error due to arguments presented in function par()

#include<iostream>
#include<string.h>
using namespace std;
int par(char s[][80],int,int,char);
char* substr(char*,int,int);
int main()
{
  char s[][80]={{"this is rockstar"},{"I am rockstar"},{"the best one"},{"no one can dare"},{"rockstar rocks always"}};
  char word[80]={"rockstar"};
  int n1=5;
  int num1=0;
  cout<<par(s,n1,num1,word);
  return 0;
}
int par(char s[][80],int n1,int num1,char word[80])
{
  int k=0;
  int length_word=strlen(word);
  int t=0;
  char beg[80];
  while(t!=strlen(word))
  {
    beg[t]=word[t];
    t++;
  }
  beg[t]=" ";
  char end[80];
  char mid[80];
  mid[0]=' ';
  t=0;
  int l=1;
  while(t!=strlen(word))
  {
    mid[l]=word[t];
    l++;
    t++;
  }
  mid[l]=' ';
  t=0;
  l=1;
  end[0]=' ';
  while(t!=strlen(word))
  {
    end[l]=word[t];
    t++;
    l++;
  }

  char temp[80];
  while(k<n1-1)
  {
    int i=0;
    while(s[k][i]!='\0')
    {
      temp[i]=s[k][i];
      i++;
    }
    if(strcmp(substr(temp,1,strlen(word)+1),beg)==0)
    {
      num1+=1;
    }
    int tr;
    for(tr=2;tr<strlen(temp)-(strlen(word)+2);tr++)
    {
      if(strcmp(substr(temp,j,strlen(word)+2),mid)==0)
      {
        num1+=1;
      }
    }
    if(strcmp(substr(temp,strlen(temp)-strlen(word),strlen(word)+1),end)==0)
    {
      num1+=1;
    }
    k++;


  }
  return num1;
}
char* substr(char *s,int i, int j)
{
  int pos=i-1;
  static char res[80];
  int k=0;
  while(pos<=i+j-2)
  {
    res[k]=s[pos];
    pos++;
    k++;
  }
  return res;
}
Striezel
  • 3,693
  • 7
  • 23
  • 37
  • Look here: https://stackoverflow.com/questions/1328223/when-a-function-has-a-specific-size-array-parameter-why-is-it-replaced-with-a-p – samuelnj Jun 03 '18 at 20:52
  • It will help if you list specifically which line you're getting the conversation error on as well. – samuelnj Jun 03 '18 at 21:14

3 Answers3

2

The issue seems to be with the declaration of par():

int par(char s[][80],int,int,char);

Here the fourth parameter is char. However, inside the main() function on the line

cout<<par(s,n1,num1,word);

you pass word, which is declared as char[80], and array easily decay to pointers in C++ and C. (See https://stackoverflow.com/a/1328246/6710751 for an explanation of why that happens.) So basically, you are passing an char[] (or char*, a pointer to char), while the compiler expects only char.

Of course, the later implementation of par() after the main() method has the correct type for the 4th parameter, but the compiler does not know about that yet when looking at the code inside main(). To fix this, you have to adjust the declaration of par() to match the implementation:

int par(char s[][80],int n1,int num1,char word[80]);

Bonus: Since this is C++ and not C, you should prefer std::string over C-style char[] for any strings. std::string is easier to handle, always knows its size (unlike an array of characters) and buffer overruns, memory leaks and the likes which are quite common in C-style string handling can be avoided.

Striezel
  • 3,693
  • 7
  • 23
  • 37
1

The 4th parameter in the declaration of method par() is of type char. The argument you pass in is char word[80].

Paul
  • 94
  • 1
  • 9
  • What? This is wrong in every way possible. The 4th parameter passed isn't type `char` it gets converted to `char *`. Also what type are you talking about `char word[80]`... – samuelnj Jun 03 '18 at 21:12
  • 1
    Well the function declaration doesn’t match the definition then – Paul Jun 03 '18 at 21:15
  • 1
    @samuelnj - what is the type of the 4th parameter in the function declaration? – Paul Jun 03 '18 at 21:29
  • Fair enough, I see what you’re trying to say so downvote removed. But I think you can clarify that it’s the function declaration so it’s helpful to the OP. – samuelnj Jun 03 '18 at 23:33
  • Thanks for the reply, the 4th parameter was the cause of the problem. –  Jun 04 '18 at 10:56
0

In the prototype of function par :

int par(char s[][80],int,int,char);

the 4th argument is of type char, whereas in the function definition of par :

int par(char s[][80],int n1,int num1,char word[80])
{...}

the 4th parameter is an aray of type char. So that's the error right there. To fix this error you need to change the 4th parameter in function prototype from char to char [].

Also, in the body of function par you have this line of code right after first while beg[t]=" ", this is wrong because you are trying to assign a string literal when it should be a character literal.


In body of function par you have this line of code if(strcmp(substr(temp,j,strlen(word)+2),mid)==0) here variable j is undefined, another error.

Hope that helps.

Tayyab Mazhar
  • 1,560
  • 10
  • 26