+ 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; }

13th Feb 2021, 1:09 AM
Edward Finkelstein
Edward Finkelstein - avatar
22 Answers
+ 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.
13th Feb 2021, 4:41 AM
Arsenic
Arsenic - avatar
+ 6
Mr.Imperfect yes not just because it's read only but you also get compiler warnings if you do so.
15th Feb 2021, 5:34 AM
Sonic
Sonic - avatar
+ 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.
13th Feb 2021, 6:23 AM
Arsenic
Arsenic - avatar
+ 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.
14th Feb 2021, 1:47 PM
Arsenic
Arsenic - avatar
15th Feb 2021, 5:39 AM
Sonic
Sonic - avatar
+ 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 }
13th Feb 2021, 5:33 AM
Arsenic
Arsenic - avatar
+ 4
Edward Finkelstein what you said about it being C++ specific is correct. No idea why someone downvoted you.
15th Feb 2021, 5:51 AM
Sonic
Sonic - avatar
+ 3
Nishant Prateek who said his code is not working ?
14th Feb 2021, 2:59 PM
Arsenic
Arsenic - avatar
+ 3
In C++ don't assign a string constant to a char*
14th Feb 2021, 8:39 PM
Sonic
Sonic - avatar
+ 3
You don't get a warning in this: https://code.sololearn.com/cj3d7vmbeEvC/?ref=app
15th Feb 2021, 5:41 AM
Sonic
Sonic - avatar
+ 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.
13th Feb 2021, 5:43 AM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
First of all your code has an error because printf() is a function of standard liberary of c language
13th Feb 2021, 7:45 PM
Nishant Prateek
Nishant Prateek - avatar
+ 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.
13th Feb 2021, 8:02 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
it seems like this is a C++ thing, in c compiler i run code with char pointer and i get no such warning.
14th Feb 2021, 7:52 PM
Edward Finkelstein
Edward Finkelstein - avatar
+ 1
Better to use cout instead of printf in C++.
14th Feb 2021, 8:40 PM
Sonic
Sonic - avatar
0
Mr. Imperfect, thanks. Why does monitor dump core when I replace cout << &p << endl; with cout << *p << endl; ?
13th Feb 2021, 2:57 AM
Edward Finkelstein
Edward Finkelstein - avatar
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
14th Feb 2021, 2:35 PM
Nishant Prateek
Nishant Prateek - avatar
0
You can declare other character variable and allocate the pointer to that variable using '&' operator and them print the output
14th Feb 2021, 2:47 PM
Nishant Prateek
Nishant Prateek - avatar
0
I mean it's showing warnings in runtime and warnings doesn't means that the code will not working
14th Feb 2021, 3:01 PM
Nishant Prateek
Nishant Prateek - avatar
0
It is for getting rid out of warnings
14th Feb 2021, 3:03 PM
Nishant Prateek
Nishant Prateek - avatar