-2

Here is my code, how can I change line 9 to line 11 in pointer form?

void ShiftRightCircular (ElemType *A, int n, int k) {
    /************** begin *****************/
    ElemType e;
    int p, i = 0;
    while (i < n - k) {
        p = i / k + 1;
        for (int j = 0; j < k ; j++) {
            e = A[j];                // line 9
            A[j] = A[ (p * k + j) % n]; // line 10
            A[ (p * k + j) % n] = e; // line 11
            i++;
        }
    }
    /************** end *****************/
}
mch
  • 9,424
  • 2
  • 28
  • 42
  • Here is my code, how can I change line 9 to line 11 in pointer form? void ShiftRightCircular(ElemType *A,int n,int k) { /************** begin *****************/ ElemType e; int p,i=0; while(i – SPING02 Apr 26 '22 at 07:57
  • 2
    Please post code as text, not as pictures of text. – Lundin Apr 26 '22 at 08:22
  • 2
    `array[index]` is 100% equivalent to `*(array + index)` – pmg Apr 26 '22 at 08:55
  • 1
    You probably do not want `e` to be a pointer. Suggestion: avoid hiding pointerness within a `typedef`. – pmg Apr 26 '22 at 08:57
  • 2
    What is `ElemType`? Please show a [mcve]. Also please tell us why you want to change lines 9 to line 11 to pointer form. Also please make clear if its `ElemType *A` like in the picture of your code or `ElemType A` like in code you posted in the comment. BTW: never post multiline code in comments, it's unreadable as you can see – Jabberwocky Apr 26 '22 at 09:06
  • Related: [Do pointers support "array style indexing"?](https://stackoverflow.com/questions/55747822/do-pointers-support-array-style-indexing) – Lundin Apr 26 '22 at 10:36

1 Answers1

0

array[index] is 100% equivalent to *(array + index)

Candidate replacement:

int d = (p * k + j) % n;  // Used to simplify the following:

e = *(A + j);        // e = A[j];
*(A + j) = *(A + d); // A[j] = A[ (p * k + j) % n];
*(A + d) = e;        // A[ (p * k + j) % n] = e;

Note: (p * k + j) % n better as (1LL * p * k + j) % n to avoid overflow.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256