Why no output? *str="12345" print str | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why no output? *str="12345" print str

https://code.sololearn.com/cLNccnBo4oCh/?ref=app char *str="12345"; - declaration of the pointer and assigning it the value 12345 instead of the address? printf("%s", *str); - must output the trash on address str="12345"?

5th Apr 2019, 6:05 AM
Sergiy L 🇺🇦
Sergiy L 🇺🇦 - avatar
7 Answers
+ 3
Zerro like ~ swim ~ explained, Sololearn outputs "no output" even when a program crashes. You'd need to use an offline compiler to see what is actually happening. In my opinion the challenge is right and wrong at the same time. The program crashes, so you could say that there is no output. But that's kinda misleading at the same time. The correct answer should be that the program crashes, instead of "no output". Assuming of course you don't have the required permission levels which you usually don't. Cause it won't crash if you do. Also by editing your answer we don't get any notification.
5th Apr 2019, 8:36 AM
Dennis
Dennis - avatar
+ 2
This is the task of sololearn challenge. the correct answer: no output It's not crashing. but I don't understand the logic
5th Apr 2019, 8:16 AM
Sergiy L 🇺🇦
Sergiy L 🇺🇦 - avatar
+ 2
#include <stdio.h> int main() { char *str="12345"; printf ("%s", *str); return 0; } Here, str stores the memory location of first character '1' (considering string as an array of characters). So, we will dereference str like this: printf("%c", *str); It will return 1 as expected. But in the code above we are using %s format specifier that will print the junk value. But Sololearn shows "no output" instead of junk value.
11th Oct 2019, 6:40 PM
Ritesh Patel
Ritesh Patel - avatar
+ 1
str = "12345"; *str = '1' = 49 So trying to print from address 49 would be an access violation and windows aborts your program. Probably a good thing, because that area is part of the interrupt jump table and you don't want that corrupted, I once accidentally overwrote part of it and my printing didn't work anymore. :( Anyway, if you where to have ring0 access, which I happen to have, then trying to do this would print 'Ô'. Well, that was boring. It just so happens that reading the hex values starting at address 49 are D4 00 F0 5E D4 00 F0 57 EF 00 F0 5E D4 00 ... etc ^^^^^^ If the string was "32345" output would be ð^Ô. There are just too many null terminators around that part to make it interesting. :(
5th Apr 2019, 8:05 AM
Dennis
Dennis - avatar
+ 1
While printing a string in C with %d, we have to pass the address of first character i.e. str should be used in place of *str. int main() { char *str = "12345"; printf(" %d",str); return 0; }
7th Apr 2019, 4:34 AM
HITESH AHUJA
HITESH AHUJA - avatar
0
Thank you, but the logic is unclear why it does not output garbage?
5th Apr 2019, 6:43 AM
Sergiy L 🇺🇦
Sergiy L 🇺🇦 - avatar
0
This output will be valid if the format is an integer. #include <stdio.h> int main() { char *str = "12345"; printf ("%lli", *str); return 0; }
5th Apr 2019, 7:06 AM
Edison
Edison - avatar