-2
void reverse(char * str){
    char * end = str;
    cout << "str" << str << endl;//ABCDE
    cout << "end" << end << endl;//ABCDE
    char tmp;
    if(str){
        while(*end){++end; cout << end << endl;}//ABCDE-->BCDE-->CDE-->DE-->E--> NULL
        --end;//end=E
        cout <<"--end" << end << endl;
        while(str<end){// do swap
            tmp = *str;//*str = str[0] 
            *str++ = *end;//*end = last ele in str[]
            *end-- = tmp;
        }
    }
}

My input is

char test[] = "ABCDE";
cout << test << endl; //ABCDE
reverse(test);
cout << test << endl; //EDCBA

I am feeling not good about the pointer, since c++ primer book says char* pointer to the first element of an array, but when I output the pointer end, it is the content of an array not the address.

Also, reverse(test), I mean to give the address of the first element in an array to the pointer, but it turns out give the whole elements to the pointer.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
flowera
  • 799
  • 8
  • 11
  • What is your question exactly ? Don't you have the issue described here ? http://stackoverflow.com/questions/36099466/exc-bad-excess-when-trying-to-reverse-a-c-style-string – Colin Pitrat Mar 19 '16 at 18:49
  • Possible duplicate of http://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed/4860812#4860812 – πάντα ῥεῖ Mar 19 '16 at 19:16

2 Answers2

1

std::cout is overloaded to print strings for char* .

Try:

char *test = "ABCDE";
std::cout << (void *) test << std::endl;
xvan
  • 4,554
  • 1
  • 22
  • 37
  • it works, but my question is that char array need to add the void* but when working on the int arrays, I just need to simply cout << test < – flowera Mar 19 '16 at 19:00
  • So what do you mean by overloaded for char *?? I mean to operate on the address of elements in an array, we need to know the address right? The char array is somehow different from integer array. – flowera Mar 19 '16 at 19:02
  • `operator<<` has a special definition when the argument is of type `char *` that definition is to print the string instead of the address... See [here](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt) for all overloads, and [here](http://en.cppreference.com/w/cpp/io/basic_ostream/operator_ltlt2) for string overloads – xvan Mar 19 '16 at 19:09
1

A char* variable is a pointer to a char. A char[] is an array of char. Now, an array of char can be accessed through a pointer, and for char* it is commonly used for string processing (it's used although for other types, but for char it's much more common).

char test[6] = "ABCDE";
char *start = &test[0]; // will point on A

Accessing the array with the pointer can be done with pointer arithmetic:

char *end = start + 5; // equivalent to char *end = &test[5]

Now when you do:

cout << test;

or

cout << start;

It's actually calling an overload of operator<< that takes a const char*. What this operator does is that it print char starting from the pointer passed until it reaches a null char ('\0').

If you want to print the address contained in the pointer and not the string, you have to cast it to void*:

cout << static_cast<void*>(start);
Colin Pitrat
  • 1,992
  • 1
  • 16
  • 28
  • Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion) E:\MyProjects\equilibrium index\string_size.cpp 139 This is the error occurs when I try to cout << &test[0], why? – flowera Mar 19 '16 at 19:08
  • Your error is strange, don't you have another definition for `test` ? Or is there something else after this on your line (more than just `cout << &test[0];`) ? – Colin Pitrat Mar 19 '16 at 19:15
  • char test[] = "ABCDEF"; cout << "-- The input add is: --" << (void*)test << endl; cout << "the add of first ele is: " << &test[0] << end; reverse(test); cout << "-- The final result is: --" << test << endl; – flowera Mar 19 '16 at 19:23
  • end instead of endl is probably your issue – Colin Pitrat Mar 20 '16 at 07:37