Sidebar Sidebar - 1 year ago 65
C Question

Porting fragment of scanf to cin

I'm trying to port my code from C to C++ but I'm stuck here:

C version:

scanf("%f", &jkr[h][*arc] + 1);

C++ version?:

cin >> jkr[h][*arc] + 1;


cin >> (jkr[h][*arc] + 1);

Answer Source

I think, the version would be

cin >> *(&jkr[h][*arc] + 1)

but if there was a programmer's license, it should be revoked immediately from anyone who writes this code.

The real problem is, what jkr is and why. It appears that it's a terrible chioce whatever it's purpose is.

You need to understand what the & operator does, it makes a pointer holding the address of it's operand. It's needed in scanf() even if you are not incrementing the pointer, but in cin it's different because I think the variable is passed by reference, because that's opssible in .

So it's reasonable to think that if, you take the address of jkr[h][*arc] increment by one, and dereference it again a reference to the value will be passed to cin::operator>>().

But the problem is that, it's too hard to understand why your code is like it is. You normally have pointers, and you do pointer arithmetic. An array of some type with 2 dimensions, used to make a pointer and do pointer arithmetic with it seems like a very bad design.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download