+ 1
C++ code
Can someone explain this C++ code? #include <iostream> using namespace std; int main() { int* p=0; int x=1; cout << p << endl; cout << &p << endl; // cout << *p << endl; doesn't work? p=&x; cout << *p << endl; cout << &x; char *ptr = "hello"; printf("\n%s",ptr); return 0; }
25 Respostas
+ 6
Edward Finkelstein the pointer "p" is a null pointer at the time of initialization.
And when you do
cout<<*p;
You are trying to dereference a NULL pointer which leads to undefined behaviour.
+ 6
Mr.Imperfect yes not just because it's read only but you also get compiler warnings if you do so.
+ 5
Edward Finkelstein
This method ( if(p) or if (p != NULL) ) checks if the pointer is pointing to null or not.
As far as I know ,there is no way to check if a pointer is pointing to valid memory address or not.
That's why we either make a pointer point to a valid memory location or make it null pointer ( pointing to nothing ) making evaluation process possible.
+ 5
Edward Finkelstein
Using "cout" should also give you output.
But using
cout<<ptr<<endl;
Will print output in previous line and then put a newline character, that's why you must have missed the output.
As for the warning is concerned then it is obviously because of the same reason it states, you are pointing that pointer to a string constant means you can only use it for read only purposes, any attempt to change it will result in error.
+ 5
You get a warning in this:
https://code.sololearn.com/c2lATVt63B7F/?ref=app
+ 4
Mr.Imperfect if it would have been any other memory address then it would have cause a problem, but 0 is a special case as it makes pointer point to NULL which is generally considers as a good practice as now you can easily test if the pointer is valid or not by doing if(p) { otherwise it would be containing a garbage value }
+ 4
Edward Finkelstein what you said about it being C++ specific is correct. No idea why someone downvoted you.
+ 3
Nishant Prateek who said his code is not working ?
+ 3
In C++ don't assign a string constant to a char*
+ 3
You don't get a warning in this:
https://code.sololearn.com/cj3d7vmbeEvC/?ref=app
+ 1
Arsenic
So int* p=0; makes p point to NULL, which evaluates to false in the expression if(p). So null pointer is not a valid pointer? This I did not know.
+ 1
First of all your code has an error because printf() is a function of standard liberary of c language
+ 1
Nishant Prateek
Do cout << ptr << endl; and you get no output, with printf you get output. The compiler says the problem/warning is the initialization of char pointer to string constant.
+ 1
it seems like this is a C++ thing, in c compiler i run code with char pointer and i get no such warning.
+ 1
Better to use cout instead of printf in C++.
0
Mr. Imperfect, thanks. Why does monitor dump core when I replace cout << &p << endl; with cout << *p << endl; ?
0
And char *ptr != "hello"
You have written char *ptr ="hello"
It not a string it is a character
That's why your code is not working
0
You can declare other character variable and allocate the pointer to that variable using '&' operator and them print the output
0
I mean it's showing warnings in runtime and warnings doesn't means that the code will not working
0
It is for getting rid out of warnings