以下是显示我想要的示例:
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/