Home > Net >  Copy data from double data type pointer to char array in c
Copy data from double data type pointer to char array in c

Time:02-15

I have a double data type pointer and size is about 512KB. I am trying to copy the data present in double data type pointer to char array.

My code looks as shown below:

double *DDR = (double *)0x90000000;
char array[524288];

How to copy the data present in DDR pointer to char array.

CodePudding user response:

Unless special restrictions apply to the memory area referred to by the absolute address 0x90000000, you can just use memcpy:

#include <string.h>

   ...
   double *DDR = (double *)0x90000000;
   char array[524288];
   memcpy(array, DDR, sizeof(array));
   ...

Alternately, you can use a loop:

   double *DDR = (double *)0x90000000;
   char array[524288];
   double *dp = (double *)array;
   for (size_t i = 0; i < sizeof array / sizeof double; i  ) {
       dp[i] = DDR[i];
   }

The compiler might actually generate the same code for both approaches.

  • Related