将短数组的值复制到长数组

标签 c

以下是显示我想要的示例:

int buf[3] = {10, 20, 30};
int * send_buf = (int *)malloc(5 * sizeof(int));

*send_buf = 1;
*(send_buf + 4) = 1;

将buf复制到send_buf后,send_buf中的值应该是:

1 10 20 30 1 

谁能让我知道在不使用 memcpy()memmove() 的情况下执行此操作的最有效方法是什么?

最佳答案

尝试以下更改 -

int buf[3] = {10, 20, 30};
int * send_buf = (int *)malloc(5 * sizeof(int));

*send_buf = 1;
for(i=1;i<=3;i++) // this will work for you. 
*(send_buf+i)=buf[i-1];
*(send_buf + 4) = 1;

关于将短数组的值复制到长数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24959713/

相关文章:

c - 特定大小的二维数组的段错误

谁能解释一下这个 CUDA 内核是如何执行的?

c - 指针到指针算术未按预期运行

c - 写的字符比 malloced 多。为什么它不会失败?

c - 一旦另一个线程运行就启动线程

java - 如果允许 JVM 在垃圾收集期间移动堆内存,那么垃圾收集如何不会因移动指针而导致 JNI 爆炸?

将十进制数组(字符串)转换为二进制数组(字节)

c - strtok() 对于解析句点但保留句点有用吗? (C)

c - 使用 pthreads 加速 C 程序