Skip to main content
Code Review

Timeline for Emulating C++ string input in C

Current License: CC BY-SA 4.0

22 events
when toggle format what by license comment
Jun 10, 2020 at 13:24 history edited Community Bot
Commonmark migration
Jun 27, 2018 at 5:21 comment added jamesqf @Toby Speight: It may be a "standard" idiom, at least among those who like to obfuscate their code, but that does not make it any less a source of potential confusion.
Jun 26, 2018 at 16:22 comment added Sandro4912 Also in the String_read method you dont initalize ch. I know it gets initalized directly in the next statement while (ch = getc(stdin) != EOF && !isspace(ch)) . However on my compiler (visual studio 2017) it terminates the programm with "Run-Time Check Failure #3 - The variable 'ch' is being used without being initialized."
Jun 26, 2018 at 16:18 comment added Sandro4912 my other question is return 0; in main also optional in c? I thought it only gets compiler generated in c++
Jun 26, 2018 at 16:16 comment added Sandro4912 the corrected code gives errors shouldnt void String_print(struct String *restrict string, FILE *restrict stream) be void String_print(struct String *restrict_string, FILE *restrict_stream)? why the restrict?
Jun 26, 2018 at 12:26 comment added Toby Speight Thanks @chux (for the link correction (I accidentally deleted the /a - oops) and for the comment that's much clearer and more accurate than what I said - yes, it's the conversion from int that matters, not the literal 0).
Jun 26, 2018 at 11:43 comment added chux 3rd link [here] is broken. Should be this
Jun 26, 2018 at 11:41 comment added chux Detail: "0 always means a null pointer" is amiss. 0 is always a constant int with value 0. Comparing that int to a pointer p, converts the int 0 to a null pointer. IAC, if (!tmp) is OK, yet as a minor point, I prefer if (tmp == NULL) as negations tend to be less clear than positive assertions - don't you not think is won't be otherwise ;-)? (Good review)
Jun 26, 2018 at 7:42 comment added Toby Speight Further reading: see the answers to Does Standard define null pointer constant to have all bits set to zero?, Why is address zero used for the null pointer? and Can I use if (pointer) instead of if (pointer != NULL)?.
Jun 26, 2018 at 7:35 comment added Toby Speight @james/hoffmale: It's a standard C (and C++) idiom to test the validity of a pointer by leaving the comparison to 0 implicit. Remember that a literal 0 always means a null pointer (whether or not the machine uses all-bits-zero for its representation). NULL doesn't "just happen" to be implemented as 0 - the Standard insists that it must be 0, even on hardware where the zero address is valid. In those cases, the implementation is required to evaluate a pointer holding the zero address as a "true" value.
Jun 26, 2018 at 7:30 comment added Toby Speight @sandro: Positive numbers are the standard convention for exit codes; most shells will truncate/convert to unsigned values - to demonstrate, you can ( exit -1 ); echo $? in Bash. So, small positive numbers ensure consistency between what you say you're returning and what your users see.
Jun 26, 2018 at 3:13 comment added hoffmale @jamesqf: That should be "happen to be implemented as 0 in your C implementation". In some implementations (e.g. embedded), 0 is actually a completely valid address.
Jun 25, 2018 at 20:33 comment added jamesqf One nit to pick. I would change "if (!tmp)" to "if (tmp == NULL)", because tmp is not a logical (true/false) condition, it's a comparison to a special value, the NULL pointer. FALSE and NULL just happen to be implemented as 0 in C, but that's not necessarily universally true.
Jun 25, 2018 at 18:35 vote accept Sandro4912
Jun 25, 2018 at 18:34 comment added Sandro4912 thanks for the in deep review. i have to admit i didn't even expected to read much comments to this code because i considered the code to easy and short :-) I was quite wrong... One question i have hwat would be an better error handling? and why exit(1) not exit(-1)
Jun 25, 2018 at 17:46 history edited Toby Speight CC BY-SA 4.0
Suggest a bigger initial size to save on mem usage (stolen from hoffmale's deleted answer)
S Jun 25, 2018 at 17:41 history suggested Andrew CC BY-SA 4.0
added missing struct reference
Jun 25, 2018 at 17:37 review Suggested edits
S Jun 25, 2018 at 17:41
Jun 25, 2018 at 17:01 history edited Toby Speight CC BY-SA 4.0
Add a lesson on invariants; various fixes
Jun 25, 2018 at 16:55 history edited Toby Speight CC BY-SA 4.0
Finish the review
Jun 25, 2018 at 16:36 history edited Toby Speight CC BY-SA 4.0
Add String_init
Jun 25, 2018 at 16:25 history answered Toby Speight CC BY-SA 4.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /