2

Currently reading into PETSc when I came up to this syntax in C/C++:

PetscInt i, n = 10, col[3], its;
PetscScalar neg_one = -1.0, one = 1.0, value[3];

I do not understand the meaning of the commas here. Has it to do with tuples? Or is there something overloaded?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Michael
  • 7,407
  • 8
  • 41
  • 84
  • 3
    If you think `PetscInt i,n = 10,col[3],its;` is as `(PetscInt i,n) = (10,col[3],its);`, then it's wrong. It is actually `PetscInt i,(n = 10),col[3],its;`. `= 10` is used to initialize `n`. – haccks Nov 02 '16 at 09:51

2 Answers2

12

That's just declaring multiple variables of the same types.

It's like

int a, b;

The first line declares four variables of the type PetscInt, called i, n (which is initialized to 10), the array col[3] and finally its. The second line declares three variables of the type PetscScalar.

So this:

PetscInt i,n = 10,col[3],its;

is the same as:

PetscInt i;
PetscInt n = 10;
PetscInt col[3];
PetscInt its;

Some find the original way shorter, easier to type, and also nice since it shows that the variables share (part of) the same type. Some find it confusing and/or error-prone, this is subjective of course but I felt I should mention it to kind of motivate why you often find code like this.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Oops...I actually used that syntax many times, but didn't see it in the statement because I confused it with Python's tuple assignment due to the layout. They'd better write `PetscInt i, n=10, col[3], its;` – Michael Nov 02 '16 at 10:00
  • It's also worth mentioning that it can be a common source of error, when someone does something like `int a, b=5;` and then misreads thinking a=5. – UKMonkey Nov 02 '16 at 10:03
  • Unfortunately they don't share the type, they share a type specifier. If they did have the same type, such declarations wouldn't be error prone when declaring multiple pointers and/or arrays. – 2501 Nov 02 '16 at 10:04
  • @2501 I tried to add the other viewpoint and also make it clear that this is subjective. – unwind Nov 02 '16 at 10:07
4

The commas here are just to declare multiple variables of the same type in a single line statement. You may very well break them into one individual line, each, like

PetscInt i;
PetscInt n = 10;
PetscInt col[3];
PetscInt its;

While both are valid and correct syntax, some of us prefer to use the broad version anyways, as it gives a better readability (IMHO) and avoid any possible confusion while having a pointer type or storage-class specifier associated with the type.

For example, for the shorthand

int * p, q;

is the same as

int *p;
int q;   //not int *q

q is not a pointer type there.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • It is worth pointing out that this style should always be used. Declaring multiple variables on the same line is a well-known source for severe bugs, particularly when pointers are involved. At the same time, the code turns easier to read. – Lundin Nov 02 '16 at 09:51
  • @Lundin I do agree with you sir, but the problem is, previously I was accused of "shoving" my personal opinion onto others for doing the same [ :( ], so I refrained from "suggesting" this time. Hope that does not affect the post in a negative way, right? – Sourav Ghosh Nov 02 '16 at 09:55
  • @Lundin However, I have expanded a bit more, trying to keep "opinon-based" content minimal, is this any good? – Sourav Ghosh Nov 02 '16 at 09:58
  • I think it's perfectly fine to point out the dangers of the syntax. If that's too much opinion-based, then Stack Overflow definitely went crazy ;-) – Michael Nov 02 '16 at 10:02
  • I think the answer was fine to begin with, I just left the comment as recommended practice to anyone reading this. – Lundin Nov 02 '16 at 10:04
  • @Lundin thanks for that and according to me, that comment is too valuable to be sitting in "comment"s section, so thought of "repairing" the answer a bit. :) – Sourav Ghosh Nov 02 '16 at 10:05