Created
December 4, 2022 19:05
-
-
Save yuri117/444c6544618760934fe915a4003cbf92 to your computer and use it in GitHub Desktop.
This is why we need memcpy() 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<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