Monika Lachowicz
Monika Lachowicz

Reputation: 23

Why I get error convert from char to char?

This is my code :

#include <iostream>
using namespace std;

bool funkcja (char * a[3][7], char * b[7]);

int main()
{

    char T[3][7]={{'A', 'L', 'A', 'M','A', 'k', 'o'},
            {'M', 'A','M','K','O','T','A'},
            {'T', 'E','Q','U','I','L','A'}};

    char tab[7]={ 'A', 'L', 'A', 'M','A', 'k', 'o' }; 

    cout<<funkcja(T, tab)<<endl;

    return 0;
}

bool funkcja (char * a[3][7], char * b[7])
{
    int licznik=0;

    for (int i=0;i<3;i++)
    {
        for (int j=0; j<7;j++)
        {
                if (a[i][j]==b[j])
                {
                    licznik++;
                    if (licznik==7) return true;
                }
                else {
                    licznik=0;
                }
        }
        licznik=0;
    }
    return false;
}

And when I'm trying to build this i get :

[Error] cannot convert 'char (*)[7]' to 'char* (*)[7]' for argument '1' to 'bool funkcja(char* (*)[7], char**)'

Upvotes: 1

Views: 121

Answers (3)

marcinj
marcinj

Reputation: 49976

You have a type mismatch between passed arguments and types of parameters in your funkcja. That was explained in other answers. In addition you can also pass arrays by reference, and the easiest way is to use templates. This way you can also pass their sizes at compile time.:

[live]

#include <iostream>
#include <string>
#include <vector>

#include <iostream>
using namespace std;

template<int N, int M>
bool funkcja (char (&a)[N][M], char (&b)[M]);

int main()
{

    char T[3][7]={{'A', 'L', 'A', 'M','A', 'k', 'o'},
            {'M', 'A','M','K','O','T','A'},
            {'T', 'E','Q','U','I','L','A'}};

    char tab[7]={ 'A', 'L', 'A', 'M','A', 'k', 'o' }; 

    cout<<funkcja(T, tab)<<endl;

    return 0;
}

template<int N, int M>
bool funkcja (char (&a)[N][M], char (&b)[M])
{
    int licznik=0;

    for (int i=0;i<N;i++)
    {
        for (int j=0; j<M;j++)
        {
                if (a[i][j]==b[j])
                {
                    licznik++;
                    if (licznik==M) return true;
                }
                else {
                    licznik=0;
                }
        }
        licznik=0;
    }
    return false;
}

Upvotes: 0

SergeyA
SergeyA

Reputation: 62553

Your funkcja accepts two arrays of pointers, yet you are passing it the arrays of chars.

You should change your signature to:

bool funkcja (char a[][7], char b[]);

Upvotes: 2

Paul R
Paul R

Reputation: 212929

Your function definition is not correct. You need to change:

bool funkcja (char * a[3][7], char * b[7])

to:

bool funkcja (char a[3][7], char b[7])

Note that you can omit the first array dimension, so this can be reduced to:

bool funkcja (char a[][7], char b[])

Upvotes: 6

Related Questions