Well, this is my program:
#include <iostream>
using namespace std;
int main()
{
char str[6][80] = { "Robert Redford",
"Hopalong Cassidy",
"Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy" };
const char* pstr[] = { "Robert Redford",
"Hopalong Cassidy",
"Lassie",
"Slim Pickens",
"Boris Karloff",
"Oliver Hardy" };
cout << "str:\n";
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 80; j++)
{
cout << str[i][j];
}
}
cout << endl;
cout << "pstr:\n";
for (int i = 0; i < strlen(*pstr) ; ++i)
{
cout << pstr[i] << endl;
}
cout << "Size of str:" << sizeof str << endl;
cout << "Size of pstr:" << sizeof pstr << endl;
return 0;
}
The problem is, the program stops running abruptly without showing the size of str and pstr and I get a message box saying that Pointers3.exe stopped working. What I want to know is, even though it prints out pstr, why does the program stop working?
Due to reputation limit I can't post images yet, but here's a screenshot for your consideration.
std::stringandstd::vectorand stay away from this pointer madness.