Answer by Peter - Reinstate Monica for C program return 1 if x > 0, 0 if x =...
It is well possible that I missed the point by assuming you can branch and simply return -1 etc. in my other answer.Here is a solution (in C++, but that doesn't matter) which constructs a value of 1, 0...
View ArticleAnswer by Peter - Reinstate Monica for Non const lvalue references cannot be...
A temporary lives, as the name suggests, only briefly. It is normally destroyed at the end of the expression of which it is a part, that is, at the next semicolon or, rarer, at the end of the...
View ArticleAnswer by Peter - Reinstate Monica for Why in C++'virtual' and '=0' is both...
The =0 marker instructs the compiler that no member function implementation is provided anywhere, and that derived classes of which objects are to be created must implement (i.e., override) it. (Of...
View ArticleAnswer by Peter - Reinstate Monica for Unwanted copy constructor call when...
Your complicated nested pointers obscure the underlying issue somewhat. Ownership is not really the issue here, and there is no issue creating a shared_ptr from a unique_ptr. The error you get concerns...
View ArticleAnswer by Peter - Reinstate Monica for How can I delete the last digit of a...
Terminal control — how to position the cursor, change colors or fonts etc. — is not part of the C++ language (and was not part of C either). However, there are a few things which work virtually...
View ArticleAnswer by Peter - Reinstate Monica for How many const variables can I declare...
Note that constants which are known at compile time may not correspond to any objects; when you compile with optimization enabled the constants will be compiled directly into the machine instructions...
View ArticleAnswer by Peter - Reinstate Monica for Compile-time string compression with...
I may have a C++17 example here with the added benefit that it does not store the string literals in the binary. I checked for pretty long arguments (the strings in line 35 pp. in the assembler are the...
View ArticleAnswer by Peter - Reinstate Monica for Compute for the income tax to be paid...
Here is an elaborate program with the following features:Accepts income of up to ca. 1.8 * 1013 units (Rupees, Dollars etc).Computes cent-exact.Accepts data from the command line or stdin.Prompts when...
View ArticleAnswer by Peter - Reinstate Monica for What is the meaning of "%-*.*s" in a...
You can read the manual page for printf. But it's more like a law text than a tutorial, so it will be hard to understand.I didn't know *.* and had to read the man page myself. It's interesting. Let's...
View ArticleAnswer by Peter - Reinstate Monica for Can an instance of a struct or a class...
Tl;dr: A variable is usually a named object, or a reference to an object. There are variables which do not have a name and there are those which do not have an associated object, but those are...
View ArticleVariable length array parameter size expression with side effects
This question arose from a remark Eric Postpischil made in another thread.I have a hard time understanding the use of variable length arrays (VLAs) as function parameters:The array size is not...
View ArticleAnswer by Peter - Reinstate Monica for Pass array to contructor and store in...
For small arrays, it's probably easiest to simply use std::array (which seems to exist on Arduino) and copy that around. That way, you don't have any lifetime issues. A std::array is basically a...
View ArticleAnswer by Peter - Reinstate Monica for I don't understand why the compiler...
The case is a bit tricky. Generally, your cast to "reference to Profesor" is a cast to an unrelated type. While you can do that, it is an error, and the result is undefined. The reason this is allowed...
View ArticleAnswer by Peter - Reinstate Monica for Why did the POSIX function...
We talk about an environment in which the system function to create a file is named creat, for heaven's sake; while it is the main error in the Unix design Ken Thompson would fix if he could do it all...
View ArticleAnswer by Peter - Reinstate Monica for Casting a void pointer in struct
Just for completeness, here is a live example. The key is to use the type tag enum to select the appropriate functions for each info type (like the following print functions) which would be virtual...
View ArticleAnswer by Peter - Reinstate Monica for Inline function linkage
It seems indeed counter-intuitive that an inline function has "external linkage" in C++ lingo; after all, the function symbol (its name) is not exported at all in the resulting object file — indeed,...
View ArticleAnswer by Peter - Reinstate Monica for Why having a function declared as...
The main effect of inline is to allow multiple definitions of the same function (because the definition is typically in a header included in multiple translation units).The name "inline" is a red...
View ArticleAnswer by Peter - Reinstate Monica for memset() to initialize object in...
The "memset approach" has two advantages over initializers for structures which are large but simple collections of data: It is clearer and has better maintainability.Clearer because it is obvious and...
View ArticleCan we change the base address of an array through a pointer to array using...
Somebody wrote the following C program and asked why gcc allows "to change the base address of an array". He was aware that the code is terrible but still wanted to know. I found the question...
View ArticleAnswer by Peter - Reinstate Monica for C_programming: Static_function_Problem
Your problem statement is not very clear. The following program compiles for me:static void f() { } // <-- your B.cvoid f(); int main() { f(); }That is the compressed version of what you did (your...
View Article