Created
April 20, 2018 16:07
-
-
Save alchem0x2A/a244b20b74efafe47b1e10fdd414b186 to your computer and use it in GitHub Desktop.
Memory copy of pointers in c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstring> //memcpy implemented here | |
// Copy pointer y from x | |
void copy_pointer(double *y, const double *x, size_t n){ | |
// The size in memcpy is byte length! | |
std::memcpy(y, x, n * sizeof(double)); | |
} | |
int main() { | |
double y[5]; | |
double x[5] = {1.0, 2.0, 3.0, 4.0, 5.0}; | |
copy_pointer(y, x, 5); | |
for (size_t i=0; i<5; ++i) | |
std::cout << x[i] << " " | |
<< y[i] << " " << std::endl; | |
// std::cout << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment