January 22, 2011
C/C++ Copy Array
For Indonesian version visit my blog at http://fsharing.blogspot.com/2011/01/cara-mengcopy-array-di-cc.html
When we create array in C/C++, we can copy one array to another by looping every element array with for or while command. Besides using this command, we can use memcpy command (we can must include string.h at header of my code). This is example how to copy array with looping and copy memory method.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
int i;
int n;
int *A=NULL, *B=NULL, *C=NULL;
n = 10;
A = (int*) calloc(n, sizeof(int));
B = (int*) calloc(n, sizeof(int));
C = (int*) calloc(n, sizeof(int));
for (i=0; i<n; i++)
A[i] = i;
for (i=0; i<n; i++)
B[i] = A[i];
memcpy(C, A, n*sizeof(int));
printf("array A\n");
for (i=0; i<n; i++)
printf("%d ",A[i]);
printf("\n");
printf("array B\n");
for (i=0; i<n; i++)
printf("%d ",B[i]);
printf("\n");
printf("array C\n");
for (i=0; i<n; i++)
printf("%d ",C[i]);
free(A);
free(B);
return (1);
}
To compile this program, I use this command :
gcc main.c -o tes
This is output from this code :
array A
0 1 2 3 4 5 6 7 8 9
array B
0 1 2 3 4 5 6 7 8 9
array C
0 1 2 3 4 5 6 7 8 9
Thank Gary for your info 🙂
7 Comments
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://www.binance.bh/futures/ref?code=IHJUI7TF
PokerNowBet, huh? New to me! Gave it a test run. Layout is pretty clean, and the odds seemed decent. Worth a look if you’re searching for something different. Dive in here: pokernowbet.
Yo, checked out jljl99app and gotta say, it’s pretty slick! The interface is smooth, and I found some cool games I haven’t seen anywhere else. Definitely worth a look if you’re bored with the usual stuff. Check ’em out here: jljl99app
Okay, so, logged in via jljl1casinologin. Pretty straightforward process. No complaints here! Time to see if I can win big, haha!
[8781]aureonplaycasino অফিসিয়াল লগইন | বিকাশ ডিপোজিট ও সেরা বোনাস,aureonplaycasino-এ আজই যোগ দিন। অ্যাপ ডাউনলোড করে অনলাইন স্লট গেমের নিয়ম জানুন এবং বিকাশ দিয়ে সহজে ডিপোজিট করে সেরা বোনাস লুফে নিন। এখনই শুরু করুন! visit: aureonplaycasino
[9382]Nikebet Login PH | Legit Online Slots gamit ang Gcash,Mag-nikebet login ph na para sa legit online slots gcash. I-download ang nikebet download APK at subukan ang aming game lobby. Register na at maglaro! visit: nikebet