C语言 将数组的子集复制到另一个数组/C 中的数组切片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14618342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:12:48  来源:igfitidea点击:

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

carrayssubsetslice

提问by fgar

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

在 C 中,是否有任何内置的数组切片机制?

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

例如在 Matlab 中,A(1:4)

would produce =

会产生 =

 1     1     1     1

How can I achieve this in C?

我怎样才能在 C 中实现这一点?

I tried looking, but the closest I could find is this: http://cboard.cprogramming.com/c-programming/95772-how-do-array-subsets.html

我试着找,但我能找到的最接近的是这个:http: //cboard.cprogramming.com/c-programming/95772-how-do-array-subsets.html

subsetArray = &bigArray[someIndex]

subsetArray = &bigArray[someIndex]

But this does not exactly return the sliced array, instead pointer to the first element of the sliced array...

但这并没有完全返回切片数组,而是指向切片数组的第一个元素的指针......

Many thanks

非常感谢

采纳答案by fgar

Thanks everyone for pointing out that there is no such built-in mechanism in C.

感谢大家指出C中没有这样的内置机制。

I tried using what @Afonso Tsukamoto suggested but I realized I needed a solution for multi-dimensional array. So I ended up writing my own function. I will put it in here in case anyone else is looking for similar answer:

我尝试使用@Afonso Tsukamoto 的建议,但我意识到我需要一个多维数组的解决方案。所以我最终编写了自己的函数。我会把它放在这里以防其他人正在寻找类似的答案:

void GetSlicedMultiArray4Col(int A[][4], int mrow, int mcol, int B[1][4], int sliced_mrow)
{
    int row, col;
    sliced_mrow = sliced_mrow - 1; //cause in C, index starts from 0
    for(row=0; row < mrow; row++)
    {
        for (col=0; col < mcol; col++)
        {
            if (row==sliced_mrow) B[0][col]=A[row][col];
        }
    }
}

So A is my input (original array) and B is my output (the sliced array). I call the function like this:

所以 A 是我的输入(原始数组),B 是我的输出(切片数组)。我这样调用函数:

GetSlicedMultiArray4Col(A, A_rows, A_cols, B, target_row);

For example:

例如:

int A[][4] = {{1,2,3,4},{1,1,1,1},{3,3,3,3}};
int A_rows = 3; 
int A_cols = 4; 
int B[1][4]; //my subset
int target_row = 1;

GetSlicedMultiArray4Col(A, A_rows, A_cols, B, target_row);

This will produce a result (multidimensional array B[1][4]) that in Matlab is equal to the result of A(target_row,1:4).

这将产生一个结果(多维数组 B[1][4]),在 Matlab 中等于 A(target_row,1:4) 的结果。

I am new to C so please correct me if I'm wrong or if this code can be made better... thanks again :)

我是 C 的新手,所以如果我错了,或者这个代码可以做得更好,请纠正我......再次感谢:)

回答by Afonso Tsukamoto

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.

在 std C 中这样做是不可能的。你必须自己做。如果你有一个字符串,你可以使用 string.h 库来处理它,但对于整数,我不知道有什么库。除此之外,在拥有了你所拥有的之后,你想要从哪里开始你的子集,实际上很容易实现。

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<arraySize;j++)
   subset[j] = originalArray[j];

Hope this helps.

希望这可以帮助。

回答by maple

In C,as far as I know, array name is just regarded as a const pointer. So you never know the size of the subset. And also you can assign a arrary to a new address. So you can simply use a pointer instead. But you should manage the size of the subset yourself.

在 C 中,据我所知,数组名只是一个常量指针。所以你永远不知道子集的大小。您也可以将一个数组分配给一个新地址。所以你可以简单地使用一个指针来代替。但是您应该自己管理子集的大小。