Hi,
If I want to pass values of one array to another array, is this the proper
code implementation. any alternative or suggestions Thanks.
.
.
.
for ( i=0; i<9; i++)
for (j =0; j<9; j++){
receive[j]=transmit[j];
}
.
.
.
Hi,
If I want to pass values of one array to another array, is this the proper
code implementation. any alternative or suggestions Thanks.
.
.
.
for ( i=0; i<9; i++)
for (j =0; j<9; j++){
receive[j]=transmit[j];
}
.
.
.
's original nested loops run i and j but the assignment only used the inner index, which either makes the outer loop redundant (if copying a 1‑D array) or is a bug (if a 2‑D copy was intended). is correct that element‑wise loops are simple and clear; 's mention of library routines points in the right direction but needs a little caution (especially for overlapping memory and non‑string data).
A few concise patterns (different from the examples already in the thread):
/* 1-D copy */
int src[9], dst[9];
for (int k = 0; k < 9; ++k)
dst[k] = src[k]; /* 2-D copy (explicit) */
int src[9][9], dst[9][9];
for (int i = 0; i < 9; ++i)
for (int j = 0; j < 9; ++j)
dst[i][j] = src[i][j]; /* block copy when memory is contiguous */
memcpy(dst, src, sizeof src); /* fast; only for non-overlapping ranges */ Key cautions and tips:
memmove instead of memcpy when source and destination may overlap. See memmove and memcpy.strcpy is for NUL‑terminated strings only; it is unsafe on non‑terminated buffers — prefer bounded forms or memcpy for fixed buffers (strcpy).sizeof on a parameter gives the pointer size, not element count. Always pass the length explicitly or use sentinel termination.Summary: loops are clear and portable (). For speed, library functions can be used when their preconditions are satisfied ().
Jump to Post— kvprajapati 1,826Please do not resurrect old threads and have a look at forum rules. Please read before posting - http://www.daniweb.com/forums/thread78223.html
Thread Closed.
That should work. Are there other ways?----there always seems to be, maybe memcpy() or something, but I always use loops.
That should work. Are there other ways?----there always seems to be, maybe memcpy() or something, but I always use loops.
Thanks for the reply bro, I will use this method.
1. memcpy
2. strcpy
3. pointer1=array1
pointer2=array2
--pointer1 =--pointer2 Please do not resurrect old threads and have a look at forum rules. Please read before posting - http://www.daniweb.com/forums/thread78223.html
Thread Closed.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.