初始化未知大小的数组(新手) [英] initialising array of unknown size (newbie)

查看:106
本文介绍了初始化未知大小的数组(新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我试图从Matlab中的编程迁移到C.我正在尝试

来创建一个简单的函数来将一个矩阵乘以其他。我已经意识到C不能确定二维阵列的大小,所以我自己输入了那些维度的尺寸。


问题是输出数组(C = A * B)的行数与A和

一样多,因为B的列数很多。我想用C初始化C: br />

double C [A_rows] [B_cols];


看起来编译器根本就不喜欢这个。


定义C的最佳方法是什么?我可以为A和B使用结构数据类型

,还是需要使用

指针来定义C的尺寸?也许malloc函数出现在某个地方。


我非常感谢任何帮助。


谢谢

Adam

解决方案

Adam Chapman< ad ********** @ student.manchester.ac.ukwrites:


我试图从Matlab中的编程迁移到C.我尝试使用

来创建一个简单的函数来将一个矩阵乘以另一个矩阵。我已经意识到C不能确定二维阵列的大小,所以我自己输入了那些维度的尺寸。


问题是输出数组(C = A * B)的行数与A和

一样多,因为B的列数很多。我想用C初始化C: br />

double C [A_rows] [B_cols];


看起来编译器完全不喜欢这个。



上面没有明显的错误。这将有助于查看代码或至少错误消息的
。特别是,如果维度是固定的并且在编译时知道
编译时,最佳

解决方案将是不同的,是固定的,但仅在运行时知道,或者如果他们是

真正的任意变量。另外,尺寸是否小?


定义C的最佳方法是什么?我可以为A和B使用结构数据类型

,还是需要使用

指针来定义C的尺寸?也许malloc函数出现在某个地方。



也许。了解您是否可以使用C99非常重要。 C99(目前的

标准)有一些功能可以让你的任务更简单。相反

关于两者的讨论,最好知道你使用/有义务使用的是什么版本的C.


你见过comp.lang.c FAQ吗? http://c-faq.com/


-

Ben。


10月9日上午11:57 * am,Ben Bacarisse< ben.use ... @ bsb.me.ukwrote:


Adam Chapman< adam.chap ... @ student.manchester.ac.ukwrites:


我试图从Matlab中的编程迁移到C.我尝试使用一个简单的函数将一个矩阵乘以另一个矩阵。我已经意识到C不能确定二维数组的大小,所以即我输入自己的维度。


问题是输出数组(C = A * B)的行数与A和

一样多许多列为B.我会考虑用以下内容初始化C:


double C [A_rows] [B_cols];


看起来编译器完全不喜欢这个。



上面没有明显的错误。 *这将有助于查看

代码或至少是错误消息。 *特别是,如果维度是固定的,并且在编译时知道
编译时,最佳

解决方案将是不同的,是固定的但仅在运行时知道,或者如果他们是

真正的任意变量。 *另外,尺寸是否小?


定义C的最佳方法是什么?我可以为A和B使用结构数据类型

,还是需要使用

指针来定义C的尺寸?也许malloc函数出现在某个地方。



也许。 *了解您是否可以使用C99非常重要。 * C99(目前的

标准)具有一些功能,可以使您的任务更简单。 *相反

关于两者的讨论,最好知道你使用/有义务使用的是什么版本的C.



我目前正在使用visual C ++ 2008编译器,但试图尽可能保持代码尽可能标准代码,以便我可以在嵌入式代码上使用它

hardwarew,类似于gumstix。


你见过comp.lang.c FAQ吗? * http://c-faq.com/


-

Ben。



感谢您快速回复。这是我目前的代码:


#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


Adam Chapman< ad ********** @ student.manchester.ac.ukwrites:
< blockquote class =post_quotes>
10月9日,11:57?* am,Ben Bacarisse< ben.use ... @ bsb.me.ukwrote:


> Adam Chapman< adam.chap ... @ student.manchester.ac.ukwrites:


我试图从Matlab编程迁移我试着用

来制作一个简单的函数来将一个矩阵乘以另一个矩阵。我已经意识到C不能确定二维数组的大小,所以即我输入自己的维度。


问题是输出数组(C = A * B)的行数与A和

一样多许多列为B.我会考虑用以下内容初始化C:


double C [A_rows] [B_cols];


看起来编译器完全不喜欢这个。


上面没有明显的错误。 ?*有助于查看代码或至少是错误消息。 ?*特别是,如果尺寸固定并且在编译时知道,固定但仅在运行时知道,或者如果它们是
,则最佳解决方案将是不同的。真正的任意变量。 ?*另外,尺寸是否小?


定义C的最佳方法是什么?我可以为A和B使用结构数据类型

,还是需要使用

指针来定义C的尺寸?也许malloc函数出现在某个地方。


也许吧。 ?*了解您是否可以使用C99非常重要。 ?* C99(当前的
标准)具有一些功能,可以使您的任务更简单。 ?*相反
关于两者的讨论,最好知道你使用/有义务使用的C版本。



我目前正在使用visual C ++ 2008编译器,但是尽量保持代码尽可能标准化,所以我可以在嵌入式

hardwarew上使用它,比如gumstix 。



好​​的,那我觉得C99已经出局了。通过瞄准C90和C99的共同交叉点,最好的便携性是

。基本上使用

" old C"但要避免任何干扰(或错误)

新标准的内容。


>你看过comp.lang.c常见问题吗? ?* http://c-faq.com/
-
本。



最好不要引用sig块。事实上,最好将你的

回复与引用的测试交错,这样你就可以对特定部分发表评论。


感谢你快速答复。这是我目前的代码:


#include< stdio.h>

#include< stdlib.h>

#include< math.h>


int Arows = 3,Acols = 3,Brows = 3,Bcols = 3,Crows,Ccols;



复制信息不是一个好主意。最好以可用于定义数组的方式定义这些。

也许:


#define AROWS 3

#define ACOLS 3

int A [AROWS] [ACOLS] = {/*...*/};
< blockquote class =post_quotes>
>

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()



这使得main和int rerturning函数(正确)但在C99中这是

" implicit int"不允许声明。写起来好多了



int main(void)


{

int row,col,i;


if(Acols!= Brows){

printf(" Can not compute; A

的列数等于B"的行数;

for(;;);



Eh?如果你有我的消息消失,除非这是

那里然后尝试找到运行程序的正确方法,这样你就可以看到它的输出(我不能帮忙 - 我根本就不知道)。
< blockquote class =post_quotes>
返回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有一个

+ =运算符,简化了这类事情。

}


}

}


for(row = 0; row< Arows; row ++)

{

for (col = 0; col< Bcols; col ++)

{

// printf("%1d",C [row] [col]);

}

printf(" \ n");

}



总的来说,我会把它分解成函数。至少

,我有一个print_matrix函数,但我也会被诱惑

把A [X] [i] * B [i ] [X]计算函数的总和。


for(;;);

返回0;

}


我得到错误C2099:初始化程序不是常数从该行

" int C = malloc(Arows * Bcols * sizeof(int));"



这是因为,在文件范围内,对象只能使用

a编译时间常量初始化 - 编译器可以使用锻炼。

对malloc的调用不是这样的。实际上,为了简单起见,C

排除了这些位置的所有函数调用,即使是那些可以由编译器计算出来的函数。


至于数组的大小,我想让我的函数能够支持任何大小的任何大小(但不足以填满所有内存),这是我整个项目的b $ b要求。我知道我没有做到这个功能

正确地接受输入,我只是让它先工作。



任意大小还不够。问题是在编译时是否可以知道大小

。它需要一套非常通用的数组

操作函数,为很多学习做好准备。你有没有看到
看看是否有一些免费的代码可用来做你需要的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 trying
to make a simple function to multiply one matrix by the other. I''ve
realised that C can''t determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as 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 type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.

I would greatly appreciate any help.

Thanks
Adam

解决方案

Adam Chapman <ad**********@student.manchester.ac.ukwrites:

Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I''ve
realised that C can''t determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as 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.

There is nothing obviously wrong with the above. It would help to see
the code or at least the error message. In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. Also, are the dimensions small?

What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.

Maybe. It is important to know if you can use C99. C99 (the current
standard) has some features that would make your task simpler. Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.

Have you seen the comp.lang.c FAQ? http://c-faq.com/

--
Ben.


On Oct 9, 11:57*am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:

Adam Chapman <adam.chap...@student.manchester.ac.ukwrites:

Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I''ve
realised that C can''t determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as 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.


There is nothing obviously wrong with the above. *It would help to see
the code or at least the error message. *In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. *Also, are the dimensions small?

What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.


Maybe. *It is important to know if you can use C99. *C99 (the current
standard) has some features that would make your task simpler. *Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.

I''m currently using the visual C++ 2008 compiler, but trying to keep
the code as standard as possible so I can use it on embedded
hardwarew, something like gumstix.

Have you seen the comp.lang.c FAQ? *http://c-faq.com/

--
Ben.

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 A
equals the number of rows of B");
for(;;);
return 0;
}
// Calculate Product
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(;;);
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 of
any size (but not huge enough to fill all the memory), this is
requirement for my overall project. I know I haven''t made the function
take inputs properly yet, I''m just getting it to work first.

Thanks
Adam


Adam Chapman <ad**********@student.manchester.ac.ukwrites:

On Oct 9, 11:57?*am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:

>Adam Chapman <adam.chap...@student.manchester.ac.ukwrites:

Im trying to migrate from programming in Matlab over to C. Im trying
to make a simple function to multiply one matrix by the other. I''ve
realised that C can''t determine the size of a 2d array, so im
inputting the dimensions of those myself.

The problem is that the output array (C=A*B) has as many rows as A and
as 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.


There is nothing obviously wrong with the above. ?*It would help to see
the code or at least the error message. ?*In particular, the "best"
solution will be different if the dimensions are fixed and know at
compile time, are fixed but known only at run time, or if they are
true arbitrary variables. ?*Also, are the dimensions small?

What is the best way to define C? could i use a structure data type
for A and B, or do the dimensions of C need to be defined using
pointers? Perhaps the malloc function comes in somewhere.


Maybe. ?*It is important to know if you can use C99. ?*C99 (the current
standard) has some features that would make your task simpler. ?*Rather
the talk about both, it would be better to know what version of C you
are using/obliged to use.

I''m currently using the visual C++ 2008 compiler, but trying to keep
the code as standard as possible so I can use it on embedded
hardwarew, something like gumstix.

OK, then I think C99 is out. The very best portability is to be had
by aiming for the common intersection of C90 and C99. Essentially use
"old C" but avoid anything that interferes with (or is wrong in) the
new standard.

>Have you seen the comp.lang.c FAQ? ?*http://c-faq.com/

--
Ben.

best not to quote sig blocks. In fact, it is best to interleave your
reply with the quoted test so you can comment on specific parts.

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;

It is not a good idea to duplicate information. It is better to
define these in a manner that can be used to define the arrays.
Maybe:

#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));

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''ll
explain the error later).

main()

This makes main and int rerturning function (correct) but in C99 this
"implicit int" declaration is not permitted. It is much better just
to write:

int main(void)

{
int row, col, i;

if (Acols!=Brows){
printf("Cannot compute; the number of columns of A
equals the number of rows of B");
for(;;);

Eh? If you are having the "my message disappears unless this is
there" then try to find the correct way to run your program so you can
see its output (I can''t help with that -- I simply don''t know).

return 0;
}
// Calculate Product
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]));

You just want to write C[row][col] = ... (i.e. no *). Also, C has a
+= operator that simplifies this sort of thing.
}

}
}

for(row=0; row<Arows; row++)
{
for(col=0; col<Bcols; col++)
{
// printf("%1d ", C[row][col]);
}
printf("\n");
}

As a general point, I''d break this up into functions. At the very
least, I''d have a print_matrix function, but I would also be tempted
to put the A[X][i] * B[i][X] sum calculation into a function.

for(;;);
return 0;
}

I''m getting "error C2099: initializer is not a constant" from the line
"int C = malloc(Arows * Bcols * sizeof(int));"

This is because, at file scope, an object can only be initialised with
a compiler-time constant -- something that the compiler can work out.
A call to malloc is not such a thing. In fact, for simplicity, C
rules out all function calls in such positions, even those that could
be worked out by the compiler.

As for the size of an array, I''d like to make my function capable of
any size (but not huge enough to fill all the memory), this is
requirement for my overall project. I know I haven''t made the function
take inputs properly yet, I''m just getting it to work first.

The "any size" is not really enough. The issue is whether the sizes
can be know at compile time. It you need a set of very general array
manipulation functions, be prepared for a lot of learning. Have you
looked to see if there is some free code available to do what you
need?

If you want fully flexible array sizes and C90 code you may well be
better off using arrays of pointers rather than 2D arrays. If there
is any chance that you might have to change the representation of your
arrays from one form to the other (maybe because of performance
issues) it could well pay to start off to use an opaque type right
from 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.


这篇关于初始化未知大小的数组(新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆