Hi,
as I compile the following code, I dont get an error during compilation, but during execution.
If I leave the string pointer "char *str1" uncommented, it runs fine.
My question is: why can't I manipulate this string pointer as a normal array? Is there a way to do so?
If anyone could give me an explanation or just a link where I could read about, it would be great.
Thank you in advice.
Code:
#include <iostream>
// turns i.e. "1234" in "4321"
void reverse(char *p)
{
for(int i = 0, j = length(p)-1; i < j; i++, j--)
{
const char tmp = p[i];
p[i] = p[j];
p[j] = tmp;
}
}
int main()
{
char *str1 = "aprob";
char str[][12] = { "Test", "Paul", "Paula", "Hallo Paul!" }; // fine
for (int i = 0; i < 4; ++i)
{
reverse(str[i]);
std::cout << str[i] << std::endl;
p_reverse(str[i]);
std::cout << str[i] << std::endl;
}
reverse(str1); // Error while running!!
system("pause");
return 0;
}