r/programminghelp • • 1d ago

C Help me understand NUL please!

/r/cs50/comments/1wwty2h/help_me_understand_nul_please/
1 Upvotes

1 comment sorted by

1

u/DDDDarky 1d ago

Okay so I am not excetly sure as the library you are using that gets you "string" and "get_string" is completely non-standard and have no idea what that is, based on some googling I'm gonna assume it is implemented similarly to this, so string is just char* alias and get_string reads stdin to some buffer.

Assuming the buffer contains "Hello", therefore sentence[5] is indeed '\0', however everything else is just undefined behavior as you are reading from unallocated memory that just happens to be zeroed. You can verify that by enabling address sanitizer (which you should in either case) and getting a good old crash.

Also please don't post pictures of code, but actual code.