-2

Please help me to find out the reason for given compilation errors. If you think there is something wrong then please suggest it to me, as I'm new to programming.

Compilation error:

[Error] request for member 'size' in 'a', which is of non-class type 'char [10]'
[Error] request for member 'size' in 'b', which is of non-class type 'char [10]'

my code is:

#include<iostream>
#include<string>

using namespace std;

int m,n;

void swap(int* p,int* q)//swap
{
    int temp = *p;
    *p=*q;
    *q=temp;
}

int partition(char a[],int l,int h)//partition
{
    int pivot=a[h];
    int index=l;
    for(int i=l;i++;i<h)
    {
        if(a[i]<pivot)
        {
            swap(a[i],a[index]);
            index++;
        }
    }
    swap(a[h],a[index]);
    return(index);
}

void quick_sort(char a[],int l,int h)//sorting
{
    while(l<h)
    {
      int p_index;
      p_index=partition(a,l,h);
      quick_sort(a,p_index + 1,h);
      quick_sort(a,l,p_index - 1);
   }
}


void anargram(char a[],char b[])
{

    quick_sort(a,0,m);
    quick_sort(b,0,n);

    if(m==n)
    {
        for(int k=0;k<m;k++)
        {
            if(a[k]!=b[k])
            {
                cout<<"NO\n";
            }

        }
        cout<<"YES\n";
    }
    else
    {
        cout<<"NO\n";;
    }
}

int main()
{
    cout<<"enter the strings";
    char a[10],b[10];

        cin>>a;
        cin>>b;

    m = strlen(a);
    n= strlen(b);
    anargram(a,b);

    return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
skvatss
  • 35
  • 1
  • 1
  • 8

1 Answers1

1

C-style arrays don't provide any member functions, as these are primitive types (hence the compiler error).

For such an array you can use sizeof(a) to determine it's actual size within the same scope of it's declaration though. As soon it's decayed to a pointer, you cannot determine the actual size from it.


As yours are most probably used to represent C-style NUL terminated strings, and you probably want to use their actual size from the input, you can also use

char a[10],b[10];

cin>>a;
cin>>b;

m = strlen(a);
n= strlen(b);

I'd recommend rather to use

std::string a(10,0),b(10,0); 

instead.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • but size of array will always be 10 as it is character array of size 10..then how will i get to know the no. of characters entered by the use – skvatss Jun 06 '15 at 19:34
  • It's important to note that even though you can use `sizeof` on an array to get its size (in bytes!) it will not work as soon as the array decays to a pointer, which it will do in many cases, most commonly when passed as arguments to functions. – Some programmer dude Jun 06 '15 at 19:34
  • @skvatss You could terminate it as a string, and then use `strlen` to get the length. – Some programmer dude Jun 06 '15 at 19:35
  • @skvatss Yes, that's what's the purpose of the `10` given as the template/constructor parameter of `std::array<>`, `std::vector` respectively. – πάντα ῥεῖ Jun 06 '15 at 19:36
  • @JoachimPileborg THX for pointing out. I've fixed it. – πάντα ῥεῖ Jun 06 '15 at 19:38
  • @JoachimPileborg can u plss provide me with the code.. m not getting it..and thankss for pointing out my mistakes..:) – skvatss Jun 06 '15 at 19:41
  • m using DEV C++ compiler – skvatss Jun 06 '15 at 19:42
  • 1
    @skvatss No, we're not going to provide you with a fixed version of your code (at least I won't do so). We've given you the information you need to fix it, isn't that fairly enough? And write proper english sentences using complete words please. I'm an old fart and hate 1337. – πάντα ῥεῖ Jun 06 '15 at 19:44
  • @πάνταῥεῖ thankss fr reminding me sir!! :) i will try to find it by my own...but if still i get struck i m gonna ask u only.. – skvatss Jun 06 '15 at 19:47
  • @skvatss Edited my answer to make it clearer for you dude. – πάντα ῥεῖ Jun 06 '15 at 19:57
  • @πάνταῥεῖ sir i made the changes as told by you.but when i run the program it crashes.compilation errors are fixed – skvatss Jun 06 '15 at 20:10
  • 1
    @skvatss Yeah,well, that means you have other errors in your code. Start up your program in the debugger and find them. And please don't call me sir please, I'm an old fart punk, and don't like it, OK? – πάντα ῥεῖ Jun 06 '15 at 20:19