1

When element of character array is compared to "1" instead of '1' it shows an error

#include <bits/stdc++.h>
using namespace std;

bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second > b.second); 
}
int main() {
    int t;
    cin>>t;
    while(t--)
    {
        int N,K;
        cin>>N>>K;
        char s[N];
        vector<pair<int,int>> zero;
        int j;
        cin>>s;
        if(s[0]=='0') j=0;     // No error when I use '0' instead of "0"
        for(int i=1;i<N;i++)
        {
            if(s[i]=="1"&&s[i-1]=="0") zero.push_back(make_pair(j,i-j));    // [Error] ISO C++ forbids comparison between pointer and integer [-fpermissive]
            else if(s[i]=="0"&&s[i-1]=="1") j=i;
        }
        if(s[N-1]=="0") zero.push_back(make_pair(0,N-j));
        sort(zero.begin(),zero.end(),sortbysec);
        for(auto i=zero.begin();i!=zero.end();i++)
        {
            cout<<i->first<<" "<<i->second<<endl;
            if(K==0) break;
            if(i->first==0)
            {
                K--;
                zero.erase(i);
            }
            else 
            {
                if(K>=2)
                {
                    K-=2;
                    zero.erase(i);
                }
            }
        }
        int res=0;
        for(auto i=zero.begin();i!=zero.end();i++)
        {
           res+=i->second;
        }
        cout<<res<<endl;
    }
    return 0;
}

[Error] ISO C++ forbids comparison between pointer and integer [-fpermissive] According to error one of them is pointer and other is integer if so then which of them is what and how?

Shubham Sharma
  • 141
  • 1
  • 9
  • Also, not directly related to the question, but worth to read: [Why should I not #include ?](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) (and they are particularly bad when combined). – Yksisarvinen Sep 21 '20 at 10:01

1 Answers1

3

Are "1" and '1' different in Cpp ?

Yes.

if yes then how?

First is a null terminated array of chars and the latter is a char.

According to error one of them is pointer and other is integer if so then which of them is what and how?

Technically, one of the operands is an array as I mentioned. But when an array is used as an rvalue, it implicitly converts into a pointer to first element. So, "1" is the pointer after the conversion. s[0] is the char.

eerorika
  • 232,697
  • 12
  • 197
  • 326