I'm a beginner on C and I don't understand all features of this beautiful language yet. So, I have a very strange problem which doesn't affect my solution at all, I anyway get the right result.
So, the task is:
Given an array of integers.
Return an array, where the first element is the count of positives numbers and the second element is sum of negative numbers. 0 is neither positive nor negative.
If the input is an empty array or is null, return an empty array.
Example: For input [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15], you should return [10, -65].
My solution:
void count_positives_sum_negatives( int *values, size_t count, int *positivesCount, int *negativesSum) { while(count-- > 0 ? (*values > 0 ? ++*positivesCount : (*negativesSum += *values)) + values++ : 0); } 'count' contains size of array
But it gives me this "error" or "warning", which strangely doesn't affect my program at all:
solution.c:6:94: warning: unsequenced modification and access to 'values' [-Wunsequenced] while(count-- > 0 ? (*values > 0 ? ++*positivesCount : (*negativesSum += *values)) + values++ : 0); ~~~~~~ ^ 1 warning generated. How do I fix this?
FIXED:
while(count-- > 0 ? (*values > 0 ? ++*positivesCount : (*negativesSum += *values)) + 1 : 0) values++;
*valuesandvalues++, so there's no way to say which operation will be evaluated first.(*values > 0 ? ++*positivesCount : (*negativesSum += *values)) + values++. Which must evaluate first?(*values > 0 ? ++*positivesCount : (*negativesSum += *values))orvalues++??:here, instead of a more conventionalif/elsestatement? There are good uses for?:, but this doesn't strike me as one of them.