开发者

equivalent expression for a[j++] = ++i without using pre or post increment operators

So I am pondering this question (this is a homework/exam review problem):

Write down an equivalent expression for a[j++] = ++i; without using pre/post increment operators. If no such expression can be provided explain why.

I was able to come up with the following:

a[j] = i+=1;

j+=1;

I can't thi开发者_StackOverflow中文版nk of a way to increment j within a[] as a post increment other than using j+=1; afterwards which I believe would lead to the answer of no such expression can be provided (because its two lines of code instead of one) and just explain that you can't post increment without the post increment operator.

Am I missing anything or am I correct? I just wanted to double check. Thanks in advance.

EDIT: Thanks to @James McNellis he provided a way using a[(j+=1)-1] = (i+=1);


This is horrible and ugly, but here it is anyway:

a[(j += 1) - 1] = (i += 1);


If you know that i won't wrap around to zero, one solution that comes to mind is this:

(a[j] = i += 1) && (j += 1);


Does the comma operator count?

a[j] = i + 1, j += 1, i += 1;

It's like three separate lines of code... but technically one. (I know the second i += 1 is unnecessary, but I wrote it for consistency.)


Equivalent expressions that don't even use += are

(a[j] = i = i + 1, j = j + 1, a[j-1])

and

(i = i + 1, j = j + 1, a[j-1] = i)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜