1

I am exploring the work on cstring inside a C++ code. One of the functions that is commonly used is strrev().

But I found that this function is considered undefined on Mac & Linux, even if I included the header file <cstring> or <string.h>.

Screenshot of the error on my computer

However, the function works fine on Windows.

(I am aware that I can define the function myself as the solution presented in here strrev not available on Linux)

This might be a very naive question, but aren't the definitions of header files supposed to be the same across different platforms? or does each platform have a different definition of the header file?

Another weird thing that is I did not find strrev() in the description of the <cstring> header file (cstring header file description), but if I don't include this header file on Windows I get an error and the error is resolved when I include it.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Wanderer
  • 1,065
  • 5
  • 18
  • 40
  • Not weird at all, there is no `strrev` function in the C-standard. MS may provide one, but otherwise it is up to you to write (which takes about 4-lines of code) – David C. Rankin May 02 '18 at 05:50
  • Here's a function available on Linux but not on Windows: [`strfry`](http://man7.org/linux/man-pages/man3/strfry.3.html) – melpomene May 02 '18 at 05:58

3 Answers3

7

One of the functions that is commonly used is strrev().

Actually, other than in programming classwork, I can't think of any viable reason why strrev() would be useful. I don't think I've had a need for such a beast in my entire multi-decade career :-)

The reason it's not defined in some platforms is because it's not mandated by the C++ standard.

The reason why it's defined in some other implementations is because the standard dictates what must to be made available, not what mustn't. In other words, implementations are allowed to include it, they're just not required to include it.

An implementation can choose to include all sorts of wonderful extra stuff, provided it doesn't break any of the stuff mandated by the standard.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
1

It's an implementation specific decision to include certain functions other than the standard ones.

However, in C++, you may use std::reverse from <algorithm> header to reverse the string.

Here's an example:

#include <iostream>
#include <algorithm>

int main()
{
    char s[] = "Hello World!";

    std::reverse( std::begin(s), std::end(s) );

    for ( const auto& i : s )
    {
        std::cout << i;
    }

    return 0; 
}

Output string:

!dlroW olleH
Azeem
  • 11,148
  • 4
  • 27
  • 40
0

here is a link where you shall find the list of function & class that should be in the header according to the standard : http://www.cplusplus.com/reference/

About the question on the header itself, C++ standard does not propose one uniformed header, but the list of what is required.

And the definitions in the headers will even vary for the same compiler, from one target to another (32 or 64 bits, etc.. ). If you check the iostream header for GCC, for instance, it includes 3 files :

#include <bits/c++config.h>
#include <ostream>
#include <istream>

the header under the bits/ directory usually vary from one target to another, and you don't directly include it in your own application.

Pellekrino
  • 346
  • 2
  • 14