With your allocation pattern, you can't do a simple copy because of the multiple allocations in the second loop. It depends on what you're really after. You can arrange things so that you do just one allocation of a set of pointers — by setting up pointers into your current 1D array so that you can access it as a 2D array. Alternatively, you can create a copy of your 1D array and then set up the pointers (2 allocations, 1 copy, 1 initialization loop). Since the only issue is whether to copy the original allocation, the main answer will work on the original data.
You have:
int N, A, B;
A = ...;
B = ...;
N = A * B;
double *a_1d = malloc(N * sizeof(double));
...check and load a_1d...
Now you can create a 2D 'array' by:
double **a_2d = malloc(A * sizeof(double *));
...check that allocation succeeded...
for (int i = 0; i < A; i++)
a_2d[i] = &a_1d[i * B];
With this initialization done, you can now use:
for (int i = 0; i < A; i++)
for (int j = 0; j < B; j++)
a_2d[i][j] = ...;
If you really need to duplicate the original a_1d
array, you'd add:
double *a_1d_copy = malloc(N * sizeof(double));
...check that the allocation succeeded...
memmove(a_1d_copy, a_1d, N * sizeof(double));
and then you can reference a_1d_copy
instead of a_1d
in setting up a_2d
.
Remember that when it comes time to free things, you need to release both a_1d
and a_2d
(and a_2d_copy
if you're using that).