-2

I have this:

void duplication(int **tab1, int **tab2, int n, int m)

The goal is to copy tab1 into tab2, so i will get the same two tables.

How can i do this, because i know that using tab2 = tab1 is not working in my case :)

Sorry if it's a stupid question, i'm actually learning C!

Thank you.

rashok
  • 12,790
  • 16
  • 88
  • 100
Alkatell
  • 45
  • 2
  • 9
  • 2
    `tab2 = tab1` is valid C, just that it probably won't do what you want in this case. – nhahtdh Jan 25 '13 at 18:21
  • 2
    It is better to pass `tab1` as a `const` if you want to change only `tab2`. – Maroun Jan 25 '13 at 18:24
  • If possible, make the target the first argument; assignment, initialization, `memcpy()`, and `strcpy()` all put the destination on the left and the source on the right. And what exactly do these `int**`s point to? How was the existing data allocated? An `int**` is a pointer to a pointer; that doesn't give enough information by itself. – Keith Thompson Jan 25 '13 at 18:29

3 Answers3

2

assuming n and m are the dimensions, and you know which one applies to which dimension, you need a double loop something like this:

for ( int x = 0; x < n; ++x )
{
    for ( int y = 0; y < m; ++y )
    {
        tab2[x][y] = tab1[x][y];
    }
}

Thats a couple of big assumptions I am making given the documentation of the function you provide, but hopefully the code gives you an idea of what needs to be done, and you can substitute the correct variables in the correct places.

For further reference, the term associated with this kind of data copying is what is called a "Deep copy". When dealing with pointers, copying the pointer value is typically insufficient, and frequently unsafe. If you would like more references on this type of code, search around for "Deep copy"

Dan F
  • 17,654
  • 5
  • 72
  • 110
  • 2
    That's assuming that `tab2` points to an already allocated array of pointers, and that each of those pointers points to an already allocated array of `int`s. – Keith Thompson Jan 25 '13 at 18:27
  • @KeithThompson Yeah, like I said, a few BIG assumptions about the function and its parameters – Dan F Jan 25 '13 at 18:28
0

You can do with the nested for loop. Usually duplication of any variable, memory alloction for the duplicated variable should be done inside that function. Duplication function should be like below.

int ** dup_tab(int **sr_table, int n, int m)
{
     int **dest_tabl = NULL;
     int i = 0;
     int j = 0;
     dest_tabl = malloc(sizeof(int *) * n);
     for (i = 0; i < n; i++)
     {
         dest_tabl[i] = malloc(sizeof(int) * m);
     }

     for (i = 0; i < n; i++)
     {
         for (j = 0; j < m; j++)
         {
             dest_tabl[i][j] = sr_table[i][j];
         }
     }
}
rashok
  • 12,790
  • 16
  • 88
  • 100
0

You should establish a point of view with the underlying memory 'management' in mind.

Probably you are looking for a function like

// defined in <string.h> and conform to C99

void memcpy(void *dest, const void *src, size_t n); 

which copies n bytes from the beginning of src to the beginning of dest.

Related to your question, if you want to use this function you have to know the size N of the object where *tab1 points to and the size of each object M_i where tab[i] points to (with i < N). If you have this information you can solve your problem according to

C / C++ How to copy a multidimensional char array without nested loops?

Community
  • 1
  • 1
user1146332
  • 2,630
  • 1
  • 15
  • 19