In the following part of the string swap code
end = &str[len - 1];
I am not understanding the addressing part. When I do it without the addressing part it still runs but gives me a warning that "a values of type char cannot be assigned to identity of type char". Here is the full code:
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str[] = "This is a test";
char *start, *end;
int len;
int t;
cout << "Original " << str << "\n";
len = strlen(str);
start = str;
end = str[len - 1];
//this reverses the string
while (start < end) {
t = *start;
*start = *end;
*end = t;
start++;
end--;
}
cout << "Reversed" << str << "\n";
system("PAUSE");
return 0;
}
Given
then
end
is pointer to char, andYou must use the
&
(address of) operator becauseend
is pointer and so it must be assigned to the address of something (here the address of the last character of the string).I don't think it will - you should have got a compile error