|
-
June 10th, 2013, 03:58 PM
#16
Re: C-String problem
 Originally Posted by syzn
woah, thanks for all the info. I don't know some of the code both of you posted, but I'll figure it out. I haven't learned much yet, hence, the horrible code I made.
How are you learning c++? You may like to look at these sites for more info
http://www.learncpp.com/
http://www.cplusplus.com/doc/tutorial/
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 10th, 2013, 04:46 PM
#17
Re: C-String problem
I have a book called C++ Early Objects. I'll look into those links as well though, thank you.
-
June 10th, 2013, 04:55 PM
#18
Re: C-String problem
 Originally Posted by syzn
I have a book called C++ Early Objects. I'll look into those links as well though, thank you.
Is that book the one by Gaddis, Walters & Muganda?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 10th, 2013, 05:01 PM
#19
-
June 11th, 2013, 04:54 PM
#20
Re: C-String problem
 Originally Posted by 2kaud
Code:
void lastChar(char const* str)
{
char const *const last = strchr(str, '\0');
(last > str) ? std::cout << *(last - 1) : std::cout << "null";
}
In addition to laserlight's post #10, I don't see any advantage in using the ternary operator here instead of a plain if-else. The result of the operation is discarded anyway.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
-
June 12th, 2013, 03:36 AM
#21
Re: C-String problem
 Originally Posted by Eri523
I don't see any advantage in using the ternary operator here instead of a plain if-else. The result of the operation is discarded anyway.
Guilty as charged In mitigation I was trying to identify a polymorphism bug at the time and I originally wrote
Code:
//Don't try this at home
std::cout << (last > str) ? *(last - 1) : "null";
but this doesn't compile of course! (incompatible operand types) So I just quickly changed it to
Code:
(last > str) ? std::cout << *(last - 1) : std::cout << "null";
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 12th, 2013, 06:08 AM
#22
Re: C-String problem
 Originally Posted by 2kaud
Code:
//Don't try this at home
std::cout << (last > str) ? *(last - 1) : "null";
In fact I considered suggesting that very variation to make the ternary operator at least half-way useful here, but came to the conclusion that it wouldn't compile before finishing my post.
I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.
This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|