最好的方法,从C中的数组创建一个子阵列 [英] Best method to create a sub array from an array in C
本文介绍了最好的方法,从C中的数组创建一个子阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数组说 A [3] = {1,2,5}
。我要创建另一个数组 A2 [2] = {2,5}
。
我曾尝试是刚刚创建一个新的数组 A2 []
,只是复制一切从需要的位置范围内的元素。
有没有其他的方法用C来完成这个?
解决方案
的memcpy(A2,和放大器;一个[1],2 * sizeof的(* a)条);
I have an array say a[3]={1,2,5}
. I have to create another array a2[2]={2,5}
.
What I have tried is to just create a new array a2[]
and just copy all the elements from the required position range.
Is there any other method to accomplish this in C?.
解决方案
memcpy(a2, &a[1], 2*sizeof(*a));
这篇关于最好的方法,从C中的数组创建一个子阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文