The reason why the compiler is complaining at
p = 50
is because you're trying to assign an integer to a
pointer to an integer. You're assigning a different type and so the compiler rightfully knows they should be incompatible.
you can fix this problem by casting 50 to a pointer via
C-style cast like so:
int* pint= (int*)50;
However, doing this is dangerous as you could be fiddling with memory you don't know. A
static_cast is also a good alternative, you should look into it.
using namespace std; Is bad practice because you are practically importing the whole standard namespace, thus opening up a lot of possibilities for name clashes. It is better to import only the stuff you are actually using in your code, like the
cout and
cin .
Use the prefix
std:: for every function you may need to use.
std::cout << "This is considered better practice than \"using\"!" << std::endl;
If this sounds too tedious and you absolutely
know you don't have any functions named
cout,
cin, or
endl, you can import them from the standard namespace like so:
1 2 3
|
using std::cout;
using std::cin;
using std::endl;
|