Copying a subset of an array into another array / array slicing in C

前端 未结 3 720
面向向阳花
面向向阳花 2020-12-16 16:58

In C, is there any built-in array slicing mechanism?

Like in Matlab for example, A(1:4)

would produce =

 1     1     1     1
<
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-16 17:51

    Doing that in std C is not possible. You have to do it yourself. If you have a string, you can use string.h library who takes care of that, but for integers there's no library that I know. Besides that, after having what you have, the point from where you want to start your subset, is actually easy to implement.

    Assuming you know the size of your 'main' array and that is an integer array, you can do this:

    subset = malloc((arraySize-i)*sizeof(int)); //Where i is the place you want to start your subset. 
    
    for(j=i;j

    Hope this helps.

提交回复
热议问题