1

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;
  }


sayeedk06
  • 103
  • 2
  • 9
  • 2
    Presumably `graph[x][0]` contains some value of a type that supports the increment operator. Maybe an int? Without seeing the definition of `graph` it's hard to say for sure. – jkb Dec 25 '19 at 06:43
  • 1
    https://en.wikipedia.org/wiki/Increment_and_decrement_operators – TruthSeeker Dec 25 '19 at 06:44
  • 1
    Unrelated: I recommend sanitizing `x` and `y` before using them as array indices. The foolish or malicious user can cause a great deal of harm if you don't make absolutely certain what they've provided is in range. In general, always make sure you A) got input (check `scanf`'s return code as it may have failed) and B) the value of the input is in a range you can use. – user4581301 Dec 25 '19 at 07:01
  • Is this code from a crackme or similar? – michi7x7 Dec 25 '19 at 08:12

3 Answers3

4

++ 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.

CinCout
  • 9,486
  • 12
  • 49
  • 67
0

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++?

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

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

  • To use the name of the array, which represents a fixed, constant, pointer value (not object) that points to the address of it first element in the array,
  • and applying it the 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.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31