begin() function on pointers/arrays

closed account (SECMoG1T)
Hi am having problems understanding the difference between

a.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
 #include <iostream>
  #include <algorithm>

  using namespace std;

  int main()
  {
   const char p[]="hello",p1[]="hello";

   cout<<((equal(begin(p),end(p),begin(p1)))?" equal" :" not equal");
   
   return 0;
  }
 


b.
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
  #include <algorithm>

  using namespace std;

  int main()
  {
   const char *p="hello",*p1="hello";

   cout<<((equal(begin(p),end(p),begin(p1)))?" equal":" not equal");
   
   return 0;
  }


# problems are
1. i thought arrays can be explictly converted to pointers to their first
element and i am getting an error on the second example.

2. i understand that the generic equal function above uses the element type
operators to compare if elements are equal? but do c_style strings
const char p[]="hello",p1[]="hello"; really have == operator
or aren't this c_style strings?? this is because the first example is
working fine so i dont get where it gets == on const char* or does it use
strcmp()? or does it compare each element as a char using == ?

please help me understand this.
Last edited on
i thought arrays can be explictly converted to pointers to their first
element and i am getting an error on the second example.
Yes, they can. Thing is, std::begin/end does not accept pointers as parameters.

but do c_style strings const char p[]="hello",p1[]="hello"; really have == operator or aren't this c_style strings??
No. Actually they do, but it compares addresses, not values.

this is because the first example is working fine so i dont get where it gets == on const char* or does it use strcmp()?
It uses neither. equal compares elements of sequence.

it works like:
1
2
3
4
5
6
while (begin1 != end!) {
    if (*begin1 != *begin2) break;
    ++begin1;
    ++begin2;
}
return begin1 == end1;

closed account (SECMoG1T)
well @minnippa you said
Thing is, std::begin/end does not accept pointers as parameters but aren't
array parameters here
1
2
3
const char p[]="hello",p1[]="hello";

   cout<<((equal(begin(p),end(p),begin(p1)))?
same as pointers or what's going
on behind the hood? what exactly does begin() take?? coz an array parameter is simply
a pointer? not sure.

but now i know how begin() works if it is as show
1
2
3
4
5
6
7
8
9
  while (begin1 != end!) 
 {
    if (*begin1 != *begin2) break;
    ++begin1;
    ++begin2;
 }
 
return begin1 == end1;
   


but confused about its args. thank you for your time and hope you i'll help
me understand what are the actuall parameters passed to begin().
aren't array parameters same as pointers
No. Your arrays have type const char[8] as opposed to const char*.

what exactly does begin() take
Overload related to arrays:
1
2
template <typename T, std::size_t N>
std::begin(T (&param)[N])


but now i know how begin() works if it is as show but confused about its args
That ws example of equal() finction, not begin(). begin1/2 is simply variable names here: bool equal(begin1, end1, begin2)
closed account (SECMoG1T)
I was very confussed, i meant That ws example of equal() finction, not begin()
equal but thanks for correction.

now i get that begin() takes a reference to an array? yeah, excellent i hope i didn't read
that the wrong way.

thank you very much @minnippa now i understand.
Topic archived. No new replies allowed.