1

So I've written a function that turns a 2D array 90 degrees and I've been told on IRC that I can't pass 2D array by reference (for example void test(char A[][10]&)) and that I just should pass my array the usual way, however when I do that, this function doesn't change the actual array. So how do I modify my original array in a function ?

void one(char A[][10], int N)
{
    char B [10][10];
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            B[j][N-i-1] = A[i][j];
    A = B;
}
Marijus
  • 4,195
  • 15
  • 52
  • 87

5 Answers5

3

A = B ; doesn't copy the elements of array B to A permanently. It's an invalid assignment to change to elements of A permanently. A retains it's original values upon function return. You need to do a member wise copy to permanently copy the elements of B to A.

Mahesh
  • 34,573
  • 20
  • 89
  • 115
1

When you pass an array (such as char A[][10]), you are actually passing a pointer to the original array, so doing A = B makes A point to B and doesn't change the original array. Instead, you can use a function such as memcpy to actually copy the contents of B to A:

memcpy(A, B, sizeof(B));
casablanca
  • 69,683
  • 7
  • 133
  • 150
1

Read suggested by @FredOverflow link: How do I use arrays in C++?.

To rotate 90° clock-wise NxN array you could divide the task in two smaller steps:

  • flip the matrix in up/down direction
  • transpose it

void rot90cw(char A[][N]) {
  // flip in up/down direction (swap rows)
  for (int i = 0; i < N/2; i++)
    std::swap_ranges(&A[i][0], &A[i][0] + N, &A[N-i-1][0]);

  // transpose (swap top-right and bottom-left triangles)
  for (int i = 0; i < N-1; i++)
    for (int j = i+1; j < N; j++)
      std::swap(A[i][j], A[j][i]);
}

I've used swap() and swap_ranges() to perform operations inplace.

Example

// -*- coding: utf-8 -*-
#include <algorithm>
#include <iostream>

namespace {
  const int N = 3;

  // rotate 90° clock-wise
  void rot90cw(char A[][N]) {
      // ... defined above
  }

  void print(char a[][N]) {
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++)
        std::cout << a[i][j];
      std::cout << '\n';
    }
    std::cout << std::endl;
  }
}

int main() {
  char a[][N] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
  print(a);
  rot90cw(a);
  std::cout << "Rotated 90° clock-wise:" << std::endl; //note: utf-8
  print(a);
}

Output

abc
def
ghi

Rotated 90° clock-wise:
gda
heb
ifc
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

Arrays don't work like that in C++. When you pass an array in a function, you are passing a pointer to the first element and nothing more, so what you are doing is creating a locally defined array B, then setting the pointer passed into your function to point to the head of the B array. At no point does the memory assigned to your original A actually change. Then when the function returns, the A pointer from your function is discarded, leaving your original A array untouched. If you want to modify an array passed as an argument to a function, you will have to modify the elements directly.

Tristan
  • 916
  • 5
  • 10
0

What you are trying to pass the array by reference and change that reference. This works, but is not necessary.

You can modify the array directly element by element:

void one(char A[][10], int N)
{
    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
        {
            char b = A[j][N-i-1];
            A[j][N-i-1] = A[i][j];
            A[i][j] = b;    
        }
}
codymanix
  • 28,510
  • 21
  • 92
  • 151