本文介绍了初始化未知大小的数组(新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图从Matlab中的编程迁移到C.我正在尝试 来创建一个简单的函数来将一个矩阵乘以其他。我已经意识到C不能确定二维阵列的大小,所以我自己输入了那些维度的尺寸。 问题是输出数组(C = A * B)的行数与A和 一样多,因为B的列数很多。我想用C初始化C: br /> double C [A_rows] [B_cols]; 看起来编译器根本就不喜欢这个。 定义C的最佳方法是什么?我可以为A和B使用结构数据类型 ,还是需要使用 指针来定义C的尺寸?也许malloc函数出现在某个地方。 我非常感谢任何帮助。 谢谢 Adam 解决方案 上面没有明显的错误。这将有助于查看代码或至少错误消息的。特别是,如果维度是固定的并且在编译时知道编译时,最佳 解决方案将是不同的,是固定的,但仅在运行时知道,或者如果他们是 真正的任意变量。另外,尺寸是否小? 也许。了解您是否可以使用C99非常重要。 C99(目前的 标准)有一些功能可以让你的任务更简单。相反 关于两者的讨论,最好知道你使用/有义务使用的是什么版本的C. 你见过comp.lang.c FAQ吗? http://c-faq.com/ - Ben。 上面没有明显的错误。 *这将有助于查看 代码或至少是错误消息。 *特别是,如果维度是固定的,并且在编译时知道编译时,最佳 解决方案将是不同的,是固定的但仅在运行时知道,或者如果他们是 真正的任意变量。 *另外,尺寸是否小? 也许。 *了解您是否可以使用C99非常重要。 * C99(目前的 标准)具有一些功能,可以使您的任务更简单。 *相反 关于两者的讨论,最好知道你使用/有义务使用的是什么版本的C. 我目前正在使用visual C ++ 2008编译器,但试图尽可能保持代码尽可能标准代码,以便我可以在嵌入式代码上使用它 hardwarew,类似于gumstix。 感谢您快速回复。这是我目前的代码: #include< stdio.h> #include< stdlib.h> #include< math.h> int Arows = 3,Acols = 3,Brows = 3,Bcols = 3,Crows,Ccols; int A [3] [3] = {1,2,3,4,5,6,7,8,9}; int B [3] [3] = {1,0, 0,0,1,0,0,0,1}; int C = malloc(Arows * Bcols * sizeof(int)); main() { int row,col,i; if(Acols!= Brows){ printf(无法计算; A /> 的列数等于B的行数); for(;;); 返回0; } //计算产品 for(row = 0; row< Arows; row ++) for(col = 0; col< Bcols; col ++) { C [row] [col ] = 0; for(i = 0; i< Acols; i ++) { * C [row] [col] = C [row] [col] +((A [row] [i])*(B [i] [col])); } } } for(row = 0; row< Arows; row ++) { for(col = 0; col< Bcols; col ++) { // printf("%1d", C [row] [col]); } printf(" \ n"); } for(;;); 返回0; } 我得到了错误C2099 :初始化程序不是常数从行开始 " int C = malloc(Arows * Bcols * sizeof(int));" 至于数组的大小,我想让我的函数能够支持任何大小(但不足以填满所有内存),这对我的整个项目来说是个b $ b要求。我知道我没有做到这个功能 正确地接受输入,我只是让它先工作。 谢谢 Adam 上面没有明显的错误。 ?*有助于查看代码或至少是错误消息。 ?*特别是,如果尺寸固定并且在编译时知道,固定但仅在运行时知道,或者如果它们是,则最佳解决方案将是不同的。真正的任意变量。 ?*另外,尺寸是否小? 也许吧。 ?*了解您是否可以使用C99非常重要。 ?* C99(当前的标准)具有一些功能,可以使您的任务更简单。 ?*相反关于两者的讨论,最好知道你使用/有义务使用的C版本。 我目前正在使用visual C ++ 2008编译器,但是尽量保持代码尽可能标准化,所以我可以在嵌入式 hardwarew上使用它,比如gumstix 。 好​​的,那我觉得C99已经出局了。通过瞄准C90和C99的共同交叉点,最好的便携性是 。基本上使用 " old C"但要避免任何干扰(或错误) 新标准的内容。 最好不要引用sig块。事实上,最好将你的 回复与引用的测试交错,这样你就可以对特定部分发表评论。 复制信息不是一个好主意。最好以可用于定义数组的方式定义这些。 也许: #define AROWS 3 #define ACOLS 3 int A [AROWS] [ACOLS] = {/*...*/}; > int A [3] [3] = {1,2,3,4,5,6,7,8,9}; int B [3] [3] = {1,0,0,0,1,0,0,0,1}; int C = malloc( Arows * Bcols * sizeof(int)); 这里不需要malloc。因此可以定义C: int C [3] [3]; 但你知道的!我想知道为什么你认为malloc是必需的(我会在后来解释这个错误的b $ b)。 这使得main和int rerturning函数(正确)但在C99中这是 " implicit int"不允许声明。写起来好多了 : int main(void) Eh?如果你有我的消息消失,除非这是 那里然后尝试找到运行程序的正确方法,这样你就可以看到它的输出(我不能帮忙 - 我根本就不知道)。 返回0; } //计算产品 for(row = 0;行< Arows;行++) { for(col = 0; col< Bcols; col ++) { C [row] [col] = 0; for(i = 0; i< Acols; i ++) { * C [row] [col] = C [row] [col] +((A [row] [i])*(B [i] [col])); 你只想写C [row] [col] = ...(即没有*)。此外,C有一个 + =运算符,简化了这类事情。 } 总的来说,我会把它分解成函数。至少 ,我有一个print_matrix函数,但我也会被诱惑 把A [X] [i] * B [i ] [X]计算函数的总和。 这是因为,在文件范围内,对象只能使用 a编译时间常量初始化 - 编译器可以使用锻炼。 对malloc的调用不是这样的。实际上,为了简单起见,C 排除了这些位置的所有函数调用,即使是那些可以由编译器计算出来的函数。 任意大小还不够。问题是在编译时是否可以知道大小 。它需要一套非常通用的数组 操作函数,为很多学习做好准备。你有没有看到看看是否有一些免费的代码可用来做你需要的b $ b $ 如果你想完全灵活的数组大小和C90代码你可能更好地使用指针数组而不是2D数组。如果有,您可能需要将 数组的表示形式从一种形式更改为另一种形式(可能是因为性能 问题)从一开始就可以开始使用不透明类型 : struct matrix * matrix_create(size_t rows,size_t cols ); element_t matrix_get(struct matrix * m,size_t row,size_t col); void matrix_set(struct matrix * m,size_t row,size_t col,element_t e) ; 所以你可以稍后更改所有细节。 - Ben。 Hi,Im trying to migrate from programming in Matlab over to C. Im tryingto make a simple function to multiply one matrix by the other. I''verealised that C can''t determine the size of a 2d array, so iminputting the dimensions of those myself.The problem is that the output array (C=A*B) has as many rows as A andas many columns as B. I would think of initialising C with:double C[A_rows][B_cols];It looks like the compiler doesn''t like this at all.What is the best way to define C? could i use a structure data typefor A and B, or do the dimensions of C need to be defined usingpointers? Perhaps the malloc function comes in somewhere.I would greatly appreciate any help.ThanksAdam 解决方案There is nothing obviously wrong with the above. It would help to seethe code or at least the error message. In particular, the "best"solution will be different if the dimensions are fixed and know atcompile time, are fixed but known only at run time, or if they aretrue arbitrary variables. Also, are the dimensions small?Maybe. It is important to know if you can use C99. C99 (the currentstandard) has some features that would make your task simpler. Ratherthe talk about both, it would be better to know what version of C youare using/obliged to use.Have you seen the comp.lang.c FAQ? http://c-faq.com/--Ben.There is nothing obviously wrong with the above. *It would help to seethe code or at least the error message. *In particular, the "best"solution will be different if the dimensions are fixed and know atcompile time, are fixed but known only at run time, or if they aretrue arbitrary variables. *Also, are the dimensions small?Maybe. *It is important to know if you can use C99. *C99 (the currentstandard) has some features that would make your task simpler. *Ratherthe talk about both, it would be better to know what version of C youare using/obliged to use.I''m currently using the visual C++ 2008 compiler, but trying to keepthe code as standard as possible so I can use it on embeddedhardwarew, something like gumstix.Thanks for te fast reply. Here is my current code:#include <stdio.h>#include <stdlib.h>#include <math.h>int Arows=3, Acols=3, Brows=3, Bcols=3, Crows, Ccols;int A[3][3]={1,2,3,4,5,6,7,8,9};int B[3][3]={1,0,0,0,1,0,0,0,1};int C = malloc(Arows * Bcols * sizeof(int));main(){int row, col, i;if (Acols!=Brows){printf("Cannot compute; the number of columns of Aequals the number of rows of B");for(;;);return 0;}// Calculate Productfor(row=0; row<Arows; row++){for(col=0; col<Bcols; col++){C[row][col]=0;for(i=0; i<Acols; i++){*C[row][col]=C[row][col] + ((A[row][i])*(B[i][col])); }}}for(row=0; row<Arows; row++){for(col=0; col<Bcols; col++){//printf("%1d ", C[row][col]);}printf("\n");}for(;;);return 0;}I''m getting "error C2099: initializer is not a constant" from the line"int C = malloc(Arows * Bcols * sizeof(int));"As for the size of an array, I''d like to make my function capable ofany size (but not huge enough to fill all the memory), this isrequirement for my overall project. I know I haven''t made the functiontake inputs properly yet, I''m just getting it to work first.ThanksAdam There is nothing obviously wrong with the above. ?*It would help to seethe code or at least the error message. ?*In particular, the "best"solution will be different if the dimensions are fixed and know atcompile time, are fixed but known only at run time, or if they aretrue arbitrary variables. ?*Also, are the dimensions small? Maybe. ?*It is important to know if you can use C99. ?*C99 (the currentstandard) has some features that would make your task simpler. ?*Ratherthe talk about both, it would be better to know what version of C youare using/obliged to use.I''m currently using the visual C++ 2008 compiler, but trying to keepthe code as standard as possible so I can use it on embeddedhardwarew, something like gumstix.OK, then I think C99 is out. The very best portability is to be hadby aiming for the common intersection of C90 and C99. Essentially use"old C" but avoid anything that interferes with (or is wrong in) thenew standard.best not to quote sig blocks. In fact, it is best to interleave yourreply with the quoted test so you can comment on specific parts.It is not a good idea to duplicate information. It is better todefine these in a manner that can be used to define the arrays.Maybe:#define AROWS 3#define ACOLS 3int A[AROWS][ACOLS] = {/*...*/};Well there is no need for malloc here. C can be defined thus:int C[3][3];but you knew that! I wonder why you thought malloc was needed (I''llexplain the error later).This makes main and int rerturning function (correct) but in C99 this"implicit int" declaration is not permitted. It is much better justto write:int main(void)Eh? If you are having the "my message disappears unless this isthere" then try to find the correct way to run your program so you cansee its output (I can''t help with that -- I simply don''t know).You just want to write C[row][col] = ... (i.e. no *). Also, C has a+= operator that simplifies this sort of thing.}As a general point, I''d break this up into functions. At the veryleast, I''d have a print_matrix function, but I would also be temptedto put the A[X][i] * B[i][X] sum calculation into a function.This is because, at file scope, an object can only be initialised witha compiler-time constant -- something that the compiler can work out.A call to malloc is not such a thing. In fact, for simplicity, Crules out all function calls in such positions, even those that couldbe worked out by the compiler.The "any size" is not really enough. The issue is whether the sizescan be know at compile time. It you need a set of very general arraymanipulation functions, be prepared for a lot of learning. Have youlooked to see if there is some free code available to do what youneed?If you want fully flexible array sizes and C90 code you may well bebetter off using arrays of pointers rather than 2D arrays. If thereis any chance that you might have to change the representation of yourarrays from one form to the other (maybe because of performanceissues) it could well pay to start off to use an opaque type rightfrom the start:struct matrix *matrix_create(size_t rows, size_t cols);element_t matrix_get(struct matrix *m, size_t row, size_t col);void matrix_set(struct matrix *m, size_t row, size_t col, element_t e);so you can change all the details at will later on.--Ben. 这篇关于初始化未知大小的数组(新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 15:22
查看更多