简体   繁体   English

数组内的增量运算符

[英]Increment operator inside array

I have a C program which does queue operations using an array.我有一个 C 程序,它使用数组进行队列操作。 In that program, they increment a variable inside array.在那个程序中,他们在数组中增加一个变量。 I can't understand how that works.我无法理解它是如何工作的。 So, please explain these operations:所以,请解释一下这些操作:

array[++i];
array[i++];

Please explain these operations.请解释这些操作。

  1. array[++i]; - first increments i , then gives you element at the incremented index - 首先增加i ,然后在增加的索引处为您提供元素

    equivalent to:相当于:

     ++i; // or i++ array[i];
  2. array[i++]; - also first increments i , but postfix operator++ returns i 's value before the incrementation - 也首先增加i ,但后缀operator++在增加之前返回i的值

    equivalent to:相当于:

     array[i]; ++i; // or i++

They increment a variable inside array.他们在数组中增加一个变量。

No, they don't.不,他们没有。 You could say they increment i within the call to array subscript operator.您可以说它们在对数组下标运算符的调用中增加i

The ++i increments i before evaluating it. ++i在评估它之前增加i

The i++ inrements i after evaluating it. i++ inrements i评估它之后。

If i=1 then array[++i] sets i=2 and then fetches array[2] .如果i=1array[++i]设置i=2然后获取array[2]

If i=1 then array[i++] fetches array[1] then sets i=2 .如果i=1array[i++]获取array[1]然后设置i=2

The post- and pre- operations happen after or before the expression they are involved in is evaluation.后操作和前操作发生在它们所涉及的表达式之后之前是求值。

I generally discourage the use of post and pre increment operators in expressions.我通常不鼓励在表达式中使用后自增运算符和前自增运算符。 They can lead to confusion at best and bugs at worst.它们充其量会导致混乱,最坏的情况是会导致错误。

Consider what x = array[++i] + array[i--] ;考虑一下x = array[++i] + array[i--] ; should be.应该。 See how easy it is to confuse the programmer ( or the poor devil who has to fix your code ? :-) ).看看让程序员(或必须修复你的代码的可怜的魔鬼?:-))感到困惑是多么容易。

Post and pre increment and decrement operations can also produce problems in macros, as you end up with the potential for an operation to be duplicated multiple times, especially with macros.后期和预递增和递减操作也会在宏中产生问题,因为您最终可能会多次重复操作,尤其是使用宏。

It is simpler and produces easier to maintain code to avoid post and pre increments in expressions, IMO.它更简单,更容易维护代码,以避免表达式中的前后增量,IMO。

So, you know i++ and ++i increment i with 1. Also, this instruction returns i , so you can put this somewhere in your code where you need the value of i .因此,您知道i++++i i 1。此外,该指令返回i ,因此您可以将其放在代码中需要i值的地方。

The difference between the 2 is that i++ is post increment, and ++i is pre increment. 2的区别在于i++是后自增, ++i是前自增。 What does this mean?这是什么意思?

Well, let's say i is 6. When you do:好吧,假设i是 6。当你这样做时:

array[i++]
array[i]

You will actually be doing:你实际上会做:

array[6]
array[7]

Because you use post increment: first return value, then increment i .因为您使用后增量:首先返回值,然后增量i

If you do:如果你这样做:

array[++i]
array[i]

You'll basically be doing:你基本上会做:

array[7]
array[7]

Because you use pre increment: first increment i , then return its value.因为您使用预增量:首先增加i ,然后返回它的值。

Now try to find what your code does ;-)现在尝试查找您的代码的作用;-)

Hope this helps.希望这可以帮助。

array[++i];数组[++i]; - increments the value of i and then uses the incremented value as an index into array - 增加 i 的值,然后使用增加的值作为数组的索引

array[i++];数组[i++]; -indexes into the array and then increments the value of i -索引到数组中,然后增加 i 的值

I know your question was in the context of a queue, but I'm going to use a stack for illustration.我知道您的问题是在队列的上下文中,但我将使用堆栈进行说明。

Imagine an array-based stack:想象一个基于数组的堆栈:

T stack[N];     // for some type T
size_t sp = N;  // stack pointer

In this example, the stack grows "downwards", where index N -1 is the bottom of the stack and index 0 is the top.在此示例中,堆栈“向下”增长,其中索引N -1 是堆栈的底部,索引 0 是顶部。

A push operation looks like the following:推送操作如下所示:

stack[--sp] = val;

--sp evaluates to sp - 1 , and as a side effect decrements the value in sp , so the above statement is equivalent to writing --sp计算结果为sp - 1 ,并且作为副作用递减sp中的值,因此上述语句等效于编写

stack[sp - 1] = val;
sp = sp - 1;

with the caveat that sp may be updated before the assignment is complete.需要注意的是,在分配完成之前可能会更新sp

A pop operation looks like this:弹出操作如下所示:

val = stack[sp++];

sp++ evaluates the the current value of sp , and as a side effect increments the value in sp , so it's equivalent to writing sp++计算sp的当前值,并作为副作用增加sp中的值,所以它相当于写

val = stack[sp];
sp = sp + 1;

with the same caveat as above.与上述相同的警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM