Skip to content

Instantly share code, notes, and snippets.

@yuri117
Created December 4, 2022 19:05
Show Gist options
  • Save yuri117/444c6544618760934fe915a4003cbf92 to your computer and use it in GitHub Desktop.
Save yuri117/444c6544618760934fe915a4003cbf92 to your computer and use it in GitHub Desktop.
This is why we need memcpy() in C
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main(){
int *A = (int *) malloc(10 * sizeof(int));
int *B = (int *) malloc(10 * sizeof(int));
int k;
scanf("%d", &k);
for (int i = 0; i < k; i++){
scanf("%d", &A[i]);
getchar();
}
memcpy(B, A, k * sizeof(int));
for(int i = 0; i < k; i++){
printf("%d ", B[i]);
}printf("\n");
B[0] = 6;
B[1] = 6;
B[2] = 6;
printf("A:\n");
for(int i = 0; i < k; i++){
printf("%d ", A[i]);
}printf("\n");
printf("\n");
printf("B:\n");
for(int i = 0; i < k; i++){
printf("%d ", B[i]);
}printf("\n");
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment