I want to know if it is possible to use the return value of a statement in C.
For example: If I start with a=0; then I know that the expression a++ will return 0 while a+=1 will return 1. But talking about statements, is there any semantic difference between a++; and a+=1;?
At first, they both look like they have similar behavior and, so, the only difference could be the existence of a way of using the return values of statements. Is it possible?
Statements don't have return values. Expressions have values (unless of type void). Some expressions also have side effects.
a++ has a value equal to the original value of a and a+=1 has a value of one greater than the original value of a.
Assigning a new value to a is a side effect. Both expressions have the same side effect.
There's nothing more complicated about any of this. Do note though, that depending on a side effect within the same statement (not just the same subexpression) produces undefined behavior, unless there is a sequence point. Multiple side effects within the same statement, and side effect dependencies, are a more advanced topic so it is better to keep assignment statements simple.
You want to compare ++a and (a+=1) which will both return 1
a++ means return the old value and then change a
++a means add one to a, and return the new value
(a+=1) means add one to a and return the new value (but technically it can be slower)
On its own line, ++a, a++ and a+=1 are all the same thing
if you call: foo(a++) and foo(a+=1) foo will be passed the different values.
Technically ++a can be faster than a+=1, but any level of optimization should result in the same thing
Related
New to C. So I saw this line of code
system->word[system->index++] = system->next_char;
Is it equivalent to:
system->word[system->index] = system->next_char;
index++;
What is the precedence for post increment? Does it only increment index's value by 1 after all the operations on the line are done executing?
Updating system->index is defined as a side effect that is not sequenced (is not specified to come before or after) the other operations in the statement. The update may occur before, during, or after other operations.
The fact that it is not sequenced is irrelevant as long as it is not used elsewhere in the statement, because, if it is not used elsewhere, then nothing the statement does can be affected by when the update occurs. (Note that, even if the update to system->index in memory is done before the value is used, the compiler is response for ensuring that the pre-update value is used.)
If the object being updated were used elsewhere in the statement in an unsequenced way (that is, no rule specifies which comes first, the update or the other use), then the behavior of the program would not be defined by the C standard.
This is not a consequence of precedence. Precedence determines the structure of how expressions are interpreted, not the sequencing of their operations.
Not.
system->word[system->index++] = system->next_char;
is equivalent to:
system->word[system->index] = system->next_char;
system->index++;
as index is a field on a struct pointed to by system. In case you have also a scalar variable named index you had had no errors but the wrong variable should have been incremented.
As a general rule, all the unary operators evaluate first on the right side of the variable, then the ones on the left (the right side operators have higher precedence thatn left side ones) and evaluate from closest to the operand, to farthest. So the operator closest to the operand evaluates first, then the one next in the right... so until there are no more right operators on the right, then the closest on the left side, and so on until there are no more left operators on the left.
So what happens if we have -x++ ? The ++ is evaluated first to the value of x and a post increment of the variable is schedule to occur, then the one on the left is evaluated, givin as result the value of x before incrementing, changed of sign, and the variable x gets its value incremented (only incremented, no sign change).
Another example: let's compute ++x->field: first of all ->field (the whole thing, the -> arrow and the field name) is considered a right unary operator, given a pointer gets the value in field named field fo structure pointed to by x. This value is incremented (and stored again in x->field) and the returned value is the final incremented one.
Another final example: let's compute *p++. The value of p is scheduled to be post incremented, but it's previous value is retained. Then the old value of p is used to access the value pointed to by it, giving the original pointed to value. Finally p is incremented and points to the value next to the value accessed.
I was answering a postgres question yesterday, and also came across a postgres thread (here) where they describe the following error:
ERROR: operator does not exist: text = text[]
HINT: No operator matches the given name and argument type(s). You
might need to add explicit type casts.
The error seems to appear whenever an ARRAY string type is fed to ANY without using = ANY. This seems completely strange since based on language, logic, and sql conventions, usually you have (e.g. IN):
variable FUNCTION(set)
instead of.
variable = FUNCTION(set) , unless ofcourse operator is a summation/count operation returning one result :)
It would make more senseto have variable ANY(Set/Array) instead of variable=ANY(Set/Array). Similar example is the IN function.
Can anyone explain what is going on here?
IN (...) is basically equivalent to = ANY (ARRAY[...])
Crucially, ANY is not a function. It's syntax defined by the SQL standard, and is no more a function than GROUP BY or the OVER clause in a window function.
The reason that = is required before ANY is that ANY can apply to other operators too. What it means is "Test the operator to the left against every element in the array on the right, and return true if the test is true for at least one element."
You can use > ANY (ARRAY[...]) or whatever. It's a general purpose operator that isn't restricted to =. Notably useful for LIKE ANY (albeit with somewhat bad performance).
There is ALL too, which does much the same thing but returns true only if all results are true.
Why unpack({0,1,1})==unpack({0,0,1}) are the same?
How to compare and proof them and proof they are different in Lua?
When a function call appears inside an expression, its return value is adjusted to one result. table.unpack({0,1,1}) == table.unpack({0,0,1}) is true because their first return value are both 0.
To compare them, iterate the tables and compare elements instead. table.pack might be helpful.
unpack is now table.unpack since Lua 5.2
Just stumbled across a bug in some old code of mine where to check if an array was empty or not I just wrote:
if my_array
...(do stuff)
end
instead of using isempty or something like that.
What I have discovered is that "if my_array" returns 0 only if the array is indeed empty OR if one or more of the components in the array are 0.
Is this expected? What exactly is going on? Is matlab performing an and operation on all the elements?
Many thanks
That is exactly the behaviour as it is documented:
An evaluated expression is true when the result is nonempty
and contains all nonzero elements (logical or real numeric). Otherwise,
the expression is false.
In c99, my understanding is that comparing two pointers which do not point within the same aggregate results in undefined behavior. Given an aggregate A, a pointer p_good which is known to point within A, and a pointer p_unknown which may or may not point within A, is it possible to construct a portable test with defined behavior which determines whether it is safe to compare p_good and p_unknown?
Obviously, this test cannot itself fall afoul of the restrictions on comparing pointers.
I suspect that the answer is 'no', but I'd be happy to be shown otherwise.
You commented:
Another way to frame the question would be like this: Given the definition of an aggregate 'A' and a pointer p, is it possible to answer the question 'does p point within A' without violating the rule on inequality testing of pointers to different aggregates
The only way I can interpret this meaningfully is that you either have an object of type Aggregate type or a pointer to one. Then the answer is simple:
Pseudo-code:
bool p_in_A = false;
for (each element in Aggregate A)
if (&element == p)
p_in_A = true;
There is no way to tell whether a stray pointer belongs to an unknown aggregate object (or points to "between" elements in an aggregate).