-5

I am new to C and C++ Programming however I saw in my Algorithms class(I transferred from a Java oriented school to a C++ oriented curriculum) that I had no idea what this meant:

*(flow + i*n + j) += minFlowPath;

I am not sure what this pointer thing is called. I want to learn the logic of what that means and what is being stored. Thank you Please redirect me.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • First of all `*(flow + i*n + j)` means that you have missed providing declaration and definition for `flow`, `i`, `n` and `j`. Please provide some context, without that relevant information, your question too broad. – Yunnosch May 30 '18 at 23:12
  • 2
    Strongly strongly STRONGLY recommend grabbing yourself [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and get familiar with C++ (not to be confused with C) or you will be totally ed when the course gets into full swing. A decent Algorithms course is hard enough. Tacking learning C++ at the same time without a good reference onto that is a good way to have to take the course twice. – user4581301 May 30 '18 at 23:30
  • If buying text books and food simultaneously is a problem, as it often is with students, here's a link to a good stop-gap since you are already familiar with programming: https://isocpp.org/tour . It's terse, but it's free. – user4581301 May 30 '18 at 23:31

1 Answers1

5

It's hard to be sure without more context, but given the expression

 *(flow + i*n + j) += minFlowPath;

it's likely that flow is a pointer. The subexpression flow + i*n + j constitutes pointer arithmetic. Addition of a pointer and an integer yields a new pointer pointing to an element counted beyond the original pointer, so here we're pointing to the element which is i*n + j elements beyond wherever flow points. (As Barmar points out in a comment, this suggests that flow is being treated as a flattened 2D array, accessing the i,jth element.)

Given any pointer or pointer-valued expression, the unary * operator access the value that the pointer points to. So

 *(flow + i*n + j)

is the value which is i*n + j past whatever flow points to.

When you access a pointer using * in this way, you get something called an lvalue, a technical term meaning that you can do more than just fetch the pointed-to value, you can also modify the pointed-to value. And that's exactly what we do here. C's += operator adds to a value, in-place. So whatever the pointed-to value was, we add the value minFlowPath to it.

To learn more about this, read up on pointer arithmetic, pointer indirection, and assignment operators.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • 5
    This is basically treating `flow` as a pointer to a 2-d array in row-major order, and incrementing `flow[i][j]`. – Barmar May 30 '18 at 23:22
  • Yes, and that's more of a C-thing to do rather than C++. In C++, you'd probably just overload []. – Lee Daniel Crocker May 30 '18 at 23:52
  • More accurately, the multiplication operator `*` has highest precedence so `i` binds to `n` and `i*n` is calculated first. From thereon, the `+` operator has left-to-right associativity, so `flow + i*n` is calculated next, the result of the expression being of same pointer type as `flow`. And then that result `+ j` is calculated, the result again being of the same pointer type. Meaning that strictly speaking `i*n + j` is never calculated. Does it matter? Well maybe, `i*n+j` could in theory give an integer overflow which won't necessarily happen with pointer arithmetic. – Lundin May 31 '18 at 06:56