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];

}
.
.
.

Dani AI

Generated

'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:

  • Use 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).
  • Inside functions, arrays decay to pointers; sizeof on a parameter gives the pointer size, not element count. Always pass the length explicitly or use sentinel termination.
  • If array elements are pointers, copying elements copies pointer values (shallow copy); allocate and copy pointed data for a deep copy.

Summary: loops are clear and portable (). For speed, library functions can be used when their preconditions are satisfied ().

Recommended Answers

All 4 Replies

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
commented: why bump this?? -1
commented: 4 years late, and for what - bunch of crap keywords without explanation -4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.