1

I have a 2D array, lets call it "A1".

A1[rows][cols].

later in my program I create another 2D array called "A2",

A2[new_rows][new_cols]

A2 is bigger than A1... is there any way for me to set A1 the same size & contents of A2?

LPs
  • 16,045
  • 8
  • 30
  • 61
Pig_Mug
  • 167
  • 3
  • 13
  • 3
    If `A1` is a static array, no you can't. If it is dynamically allocated you have chances. BTW you should post an [MCVE](http://stackoverflow.com/help/mcve) – LPs Mar 03 '17 at 16:22
  • 1
    [memcpy](http://en.cppreference.com/w/cpp/string/byte/memcpy)? See [this answer](http://stackoverflow.com/a/16896253/4175042) – Avantol13 Mar 03 '17 at 16:23
  • @Avantol13 matrices has different sizes... – LPs Mar 03 '17 at 16:24
  • Like you declare the array A1, I think you cant change it size.. Also you need to use a dynamique array. – Mouaici_Med Mar 03 '17 at 16:24
  • Possible duplicate of [How can I change the size of an array in C?](http://stackoverflow.com/questions/3827892/how-can-i-change-the-size-of-an-array-in-c) – LPs Mar 03 '17 at 16:28
  • 1
    You cannot change the size of an array after it has been defined. – John Bode Mar 03 '17 at 16:38

1 Answers1

0

Arrays are static in C, so unfortunately you cannot change the size of an array once you define it. You can, however, achieve what you speak of using dynamically allocated arrays (although, this isn't strictly the same as resizing an array since, when reallocating, you lose the reference to the original array). Start by creating two dynamically allocated arrays A1 and A2 using malloc. Next, use realloc to reallocate A1 to be the same size as A2. Finally, copy the contents of A2 to A1. This will effectively "resize" A1 to be the same size as A2 with the same contents as A2. Here is some sample code (you may use whatever populating method is right for you, I just used filler):

#include <stdio.h>
#include <stdlib.h>

int **make2DArray(int rows, int cols);
void populate2DArray(int **array, int rows, int cols);
void print2DArray(int **array, int rows, int cols);

int main(int argc, char **argv)
{
  int i, j;
  int rows = 2, cols = 3;
  int newRows = 4, newCols = 7;

  // Create two dynamic arrays.
  int **A1 = make2DArray(rows, cols);
  int **A2 = make2DArray(newRows, newCols);

  // Populate the dynamic arrays (however you like).
  populate2DArray(A1, rows, cols);
  populate2DArray(A2, newRows, newCols);

  // Print original arrays.
  printf("A1 (before):\n");
  print2DArray(A1, rows, cols);
  printf("\nA2 (before):\n");
  print2DArray(A2, newRows, newCols);

  // Reallocate A1 to be same size as A2.
  int **temp = realloc(A1, sizeof(int *) * newRows);
  if (temp)
  {
    A1 = temp;
    int *tempRow;
    for (i = 0; i < newRows; i++)
    {
      tempRow = realloc(A1[i], sizeof(int) * newCols);
      if (tempRow)
      {
        A1[i] = tempRow;
      }
    }
  }

  // Copy contents of A2 to A1.
  for (i = 0; i < newRows; i++)
  {
    for (j = 0; j < newCols; j++)
    {
      A1[i][j] = A2[i][j];
    }
  }

  // Print resized A1 (should be same as A2).
  printf("\nA1 (after):\n");
  print2DArray(A1, newRows, newCols);
  printf("\nA2 (after):\n");
  print2DArray(A2, newRows, newCols);
}

int **make2DArray(int rows, int cols) {
  // Dynamically allocate a 2D array.
  int **array = malloc(sizeof(int *) * rows);
  if (array)
  {
    for (int i = 0; i < rows; i++)
    {
      array[i] = malloc(sizeof(int) * cols);
    }
  }

  return array;
}

void populate2DArray(int **array, int rows, int cols) {
  // Populate a 2D array (whatever is appropriate).
  int i, j;
  for (i = 0; i < rows; i++)
  {
    for (j = 0; j < cols; j++)
    {
      array[i][j] = i + j;
    }
  }
}

void print2DArray(int **array, int rows, int cols)
{
  // Print a 2D array to the terminal.
  int i, j;
  for (i = 0; i < rows; i++)
  {
    for (j = 0; j < cols; j++)
    {
      printf("%d ", array[i][j]);
    }
    printf("\n");
  }
}

The output to the following code will be:

A1 (before):
0 1 2 
1 2 3 

A2 (before):
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9 

A1 (after):
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9 

A2 (after):
0 1 2 3 4 5 6 
1 2 3 4 5 6 7 
2 3 4 5 6 7 8 
3 4 5 6 7 8 9 
dwhite5914
  • 96
  • 5