本文介绍了给出一个矩阵,求行和列的数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到行和矩阵具有的列数,而无需任何其他东西的知识。

I want to find the number of rows and columns a matrix has without having knowledge of any other thing.

例如:

int * findElements(int matInput[][]) {
      /*Count blah*/
      /*Now to run a loop till the number of rows*/
      /*I need to know the size of the matrix to run the loop above*/
}

我不能运行一个循环找到的大小,我不知道什么时候终止,也不知道如果矩阵,同时创造初始化。
还有什么其他的方法?

I cannot run a loop to find the size as I don't know when to terminate and also don't know if the matrix was initialized while creation.Is there any other method?

推荐答案

您不能这样做在C.这是很根本不可能,如果没有某种形式的更多信息,可以找到特定的只是一个指向它的指针数组的大小

You cannot do this in C. It is quite literally impossible, without some kind of additional information, to find the size of an array given just a pointer to it.

这也支持查询数组的长度语言传递一些额外的信息做到这一点。在C语言中,你可以做到这一点为好,但你必须做明确的:

Languages which do support querying array length do this by passing some additional information. In C you could do this as well, but you have to do it explicitly:

struct matrix {
    int rows, cols;
    int *data; // packed representation, or int **data;
};

int *findElements(struct matrix *matInput);

作为一个稍微更先进的方法,你可以在内存中的结构矩阵后立即放置在阵列中的数据;这降低存取所需指针的数目,因此是稍快。但基本技术是相同的。

As a slightly more advanced method, you could place the array data right after the struct matrix in memory; this reduces the number of pointer accesses needed and thus is slightly faster. But the basic technique remains the same.

这篇关于给出一个矩阵,求行和列的数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:06