Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to memcpy a part of a two dimensional array in C?

Tags:

arrays

c

memcpy

How to memcpy the two dimensional array in C:

I have a two dimensional array:

int a[100][100];

int c[10][10];

I want to use memcpy to copy the all the values in array c to array a, how to do this using memcpy?

int i;
for(i = 0; i<10; i++)
{
    memcpy(&a[i][10], c, sizeof(c));
}

is this correct?

like image 446
user2131316 Avatar asked Jun 03 '13 11:06

user2131316


2 Answers

That should work :

int i;
for(i = 0; i<10; i++)
{
    memcpy(&a[i], &c[i], sizeof(c[0]));
}
like image 136
Fabien Avatar answered Nov 08 '22 17:11

Fabien


It should actually be:

for(i = 0; i < 10; ++ i)
{
  memcpy(&(a[i][0]), &(c[i][0]), 10 * sizeof(int));
}
like image 39
cgledezma Avatar answered Nov 08 '22 16:11

cgledezma