Reputation: 103
In the code below: how does the increment operator(++) work? Is it increasing the value of the array or iterating through the array somehow?
scanf("%d",&e);
for(i=0;i<e;i++)
{
scanf("%d %d",&x,&y);
graph[x][0]++;
graph[x][graph[x][0]]=y;
graph[y][0]++;
graph[y][graph[y][0]]=x;
}
Upvotes: 1
Views: 1717
Reputation: 12708
The ++
and --
operators act on scalar values, incrementing or decrementing them. An array (not a pointer) is an aggregate of values (an ordered list of values stored contiguously in memory) so it cannot be incremented or decremented.
What is confusing about this is that an array and a pointer share access methods when the []
operator is used, as the pointer is considered a scalar value and is allowed to be incremented or decremented.
So you can increment or decrement a pointer, when you do this, the pointer will point to the next (or previous) element, considering the elements are stored in array style. This is a very useful C tool, that allows to position a pointer just pointing to an array element (never confuse an array element with the whole array) and move it forward or backward, by incrementing or decrementing the pointer.
But remember, an array and a pointer are different things. An array is an aggregate of elements, while a pointer is an object that stores the address of another.
In the case you post, the postincrement operator is attached to graph[x][0]
which represents an array element (a single cell) so it is incrementing the array element positioned at position [x][0]
, and not the whole array.
Arrays are very simple in what operations they allow the programmer, the only operations they allow are
sizeof
operator, that will give you the actual size of the array. No more operations are permitted on arrays. If you try to increment or decrement this pointer, as in graph++
(or ++graph
) you'll get an error, noticing you about the fact that an array is not a pointer object, or if you take the sizeof
operator to an array, you'll get the full size of the array in bytes, while if you do this to a pointer, you'll get the size of the pointer variable, different things (normally, all pointers are the same size in a fixed architecture, but array sizes normally depend on the number of elements declared).
The []
operator is defined for pointers, and there's a trick here, to mean the same as the following equivalent expressions: a[b]
is equivalent to the expresion *(a + b)
for pointers, as you normally use the name of the array, it means to add b
to the pointer to the first element (to move the a
pointer value b
element places up in memory), and then access the pointed to value.
Upvotes: 0
Reputation: 15082
Let´s first see how the increment-operator ++
usually works:
var++
The increment operator is used to increase a value held and portrayed by a variable by 1
or 1.0
, equivalent to the statement var = var + 1;
or var = var + 1.0;
.
So it is increasing the value by 1 counter.
Now lets look on what an array is:
datatype array_of_variables[index];
An array is a bond of several variables of the same datatype. By giving the index fields we can access certain variables of this cluster.
array_of_variables[0];
Following the above statement, we access the variable which is at first in dimension 1, due to of course, this is an one-dimensional array only.
So, the expression array_of_variables[0]
is an identification for this first variable and the value held inside it.
The same goes for two-dimensional arrays. The only thing what is changed is that the "adressing" of a certain variable is now done by two index fields, according to the where the variable is stored in the dimensions.
So, array_of_variables[0][1]
equals the value held by variable 2 in dimension 1. It is an identification for it.
Now the statement,
array_of_variables[0][1]++;
does the same as done by a certain variable on its own. It is increasing the value inside the corresponding variable.
The array as whole does not get changed. Only a specific variable and the its value.
There is also an important difference whether the operator is preceding the variable ++var
or trailing at it var++
.
But I do not want to discuss that certain topic here further, it is best explained at: C: What is the difference between ++i and i++?
Upvotes: 0
Reputation: 9619
++
is a unary operator, i.e., it acts on one operand.
The statement graph[x][0]++;
should be perceived as (graph[x][0])++;
, which means that the value in the 2d array graph
at index x
and 0
for the first and the second dimension respectively is fetched, and then the operator is applied to it, thus increasing it by one.
Is it increasing the value of the array
Yes, at a specific index.
or iterating through the array somehow?
No.
Upvotes: 4