Changing a 2d array in a function

So, I wrote a function that turns a 2D array 90 degrees, and I was told by IRC that I can not pass a 2D array by reference (for example, void test (char A [] [10] &)) and that I just have to pass my array in the usual way, however, when I do this, this function does not change the actual array. So how do I change the source 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][Ni-1] = A[i][j]; A = B; } 
+1
source share
5 answers

A = B ; Does not copy elements of array B to permanently. This is an invalid assignment to permanently modify elements of A A retains its original values ​​when the function returns. You need to make a wise copy of the member in order to permanently copy elements of B to A

+3
source

When you pass an array (for example, char A[][10] ), you actually pass a pointer to the source array, so A = B makes A equal to B and does not change the original array. Instead, you can use a function like memcpy to actually copy the contents of B to A :

 memcpy(A, B, sizeof(B)); 
+1
source

Read the suggested @FredOverflow link: How to use arrays in C ++? .

To rotate the desired NxN array clockwise 90 Β°, you can divide the task into two small steps:

  • flip the matrix up / down
  • transpose him

 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[Ni-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 used swap() and swap_ranges() to perform operations in place.

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 
+1
source

Arrays do not work as they do in C ++. When you pass an array to functions, you pass a pointer to the first element and nothing more, so what you do is create a locally defined array B , and then set the pointer passed to your function to point to the head of array B. In no case The memory assigned to your original A does not change. Then, when the function returns, the pointer A from your function is discarded, leaving the original array A untouched. If you want to change the array passed as an argument to the function, you will have to directly modify the elements.

0
source

That you are trying to pass an array by reference and change that link. It works, but not required.

You can change the array directly 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][Ni-1]; A[j][Ni-1] = A[i][j]; A[i][j] = b; } } 
0
source

Source: https://habr.com/ru/post/1343389/


All Articles