-1

I newly started C++ and it feels pretty wired while writing Java for a while. So, I have this array,

char values[][10] = {"miami", "seattle", "berlin"};

int rows = sizeof values / sizeof values[0];

This is this is the function where I would like to pass the value,

// a function to reverse the strings 
void App::reverse(char *str) {

}

When I do the loop, I can't apparently pass the value there,

   for (int i = 0; i < rows; ++i) {

         // first character of the string 
        char *firstPtr = values[i];
        reverse(firstPtr);
    }

The line reverse(firstPtr) provides error which I don't understand. The error message says Too few arguments, expected 2.

What is the issue here? I apologize for any mistakes as writing the C++ for the first time and the pointer stuff feels strange.

UPDATE

This is the piece of code I would like to exexute,

void App::reverse(char* str) {

    // get the first character of the string 
    char *ptrEnd = str;
    char temp;

    if (str){

        while (*ptrEnd) {
            ptrEnd++;
        }

        ptrEnd--;

        // as long the first adddress is lesser than the end 
        while (str < ptrEnd) {

            temp = *str;
            *str++ = *ptrEnd;            
            *ptrEnd-- = temp;
        }
    }
}
Arefe
  • 11,321
  • 18
  • 114
  • 168
  • 6
    "the pointer stuff feels strange" That's why you should use `std::string` and `std::vector` and some other modern stuff instead. – Rakete1111 Aug 21 '17 at 01:51
  • Any reason why the `reverse(firstPtr)` provides errors? – Arefe Aug 21 '17 at 01:53
  • 2
    "provides error which I don't understand" - please provide the text of this error! – Ken Y-N Aug 21 '17 at 01:53
  • 2
    @Artin -- Whoever or whatever is teaching you C++ like this, run away. – PaulMcKenzie Aug 21 '17 at 01:54
  • @KenY-N I updated the question with the error which is `Red` in the `Clion` – Arefe Aug 21 '17 at 01:55
  • @PaulMcKenzie what do you mean? I learn it from a book called `Cracking the coding interview`. I do it because I have already solved these problems in `Java`, so thought to have some advantages. I also use the site `http://www.cprogramming.com/` – Arefe Aug 21 '17 at 01:57
  • 1
    @Artin -- When you say "reverse the strings", do you mean to reverse the letters in each string? If so, then this is doomed for failure from the start since you cannot change a string literal without invoking undefined behavior. – PaulMcKenzie Aug 21 '17 at 01:57
  • 2
    @Artin -- [Stop teaching C when teaching C++](http://cppcast.com/2015/10/kate-gregory/). Also, a good C++ interviewer will not like it too much if you resort to writing `C` code. Also your update proves my point -- your code was doomed for failure, even if you got it to compile. You cannot change string literals (they are basically immutable), even though the compiler lets you get away with trying to change them. – PaulMcKenzie Aug 21 '17 at 01:59
  • @PaulMcKenzie The declaration is `char values[][10]`, so they will be writable. – Ken Y-N Aug 21 '17 at 02:04
  • @Artin -- [Sample C++ code](http://ideone.com/FN4g47). – PaulMcKenzie Aug 21 '17 at 02:13
  • 1
    @KenY-N -- Point made, but it will be a surprise to the OP when they do this: `App a; a.reverse("test");` and wonder why the program goes nuts. – PaulMcKenzie Aug 21 '17 at 02:15

1 Answers1

2

There is too little information here to be sure, but it looks like you have

using namespace std;

Somewhere in your code. Don't do this! In this case, the standard library has a function reverse() in std that takes two parameters.

Furthermore, you have void App::reverse(char *str), but that cannot be seen from void myArray::reverse(char* str), so your own reverse() cannot be called as-is - you would need to do App::reverse() if the function is a class static.

Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
  • That's correct. Changed from the static to normal, called with an instance and worked. However, if the function is static and have same name function in library, how to call it then? – Arefe Aug 21 '17 at 02:01
  • @Artin I've updated my answer - `App::reverse()` will do it. – Ken Y-N Aug 21 '17 at 02:04
  • Thanks a lot, this helps me a lot – Arefe Aug 21 '17 at 02:07