试图让我的“填充值”功能工作...... [英] trying to make my "fillvalues" function work...

查看:87
本文介绍了试图让我的“填充值”功能工作......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试将matlab程序转换为c语言。对于那些知道

matlab的人来说,这是我想用c编程的行:


hx(1:nx,1:ny)= 0; %nx = 10,ny = 10


它适用于二维数组(大小为10 * 10),设置所有内部的

值10 * 10矩阵为零。我的C函数看起来像这样:

void fillinnumbers(int fillvalue,int startx,int stopx,int starty,int

stopy,int array [] [])

{

for(i = startx; i< = stopx; i ++)

{

for(j = starty; j< = stopy; j ++)

{

array [i] [j] = fillvalue;

}

}

}

因此,采用上面的例子:fillvalue = 0,startx = 1,stopx = nx = 10,

starty = 1,stopy = ny = 10,array [] []是指针......现在,

的问题是我是C新手所以我有什么问题

数组[] []的事情,我想...


在我的C程序中,我尝试过:


fillinnumbers(3,1,nx,1,ny,array);


我将数组定义为:array [nx + 1] [ny + 1]


但是使用MSVS 2005,我收到以下错误:错误C2087:''array'':

缺少下标 - 错误指向包含

函数声明的行:" void fillinnumbers(int fillvalue,.... etc"


和很多警告:警告C4048:不同的数组下标:''int

(*)[1]''和''int [11] [11]"在我尝试调用的每一行中我的功能

,例如在包含以下内容的行中:" fillinnumbers(3,1,nx,1,ny,array);"


我'对C编程没有那么有经验,所以任何帮助都会非常好

赞赏...

祝你好运/ Med venlig hilsen

Martin J?rgensen


-

------------------------- --------------------------------------------------

Martin J?rgensen的主页 - http://www.martinjoergensen.dk

推荐答案

在文章< 29 ************ @ news.tdc.dk>中,

=?ISO-8859-1?Q ?Martin_J = F8rgensen?=< un ********* @ spam.jay.net>写道:


int array [] []


你不能在C中声明一个这样的数组。


C90中有不同种类的多维数组。


其中一个是一块连续的内存地址。为了让C能够找到该区块中的正确位置,它需要知道所有尺寸的最大尺寸除了


最后一个,因为它需要做等价的

(i-1)* maxfirstdim +(j-1)才能找到
$ b的偏移量$ b内存块的开头。但是C并没有将这个维度记录为指向传递的指针的属性,所以当你将这些类型的数组中的一个传递到另一个例程中时,这是


其他例程必须声明数组大小。


在C90中,不可能传递多维的大小

数组并且声明随传递的大小而变化。


在C99中,添加了执行此操作的功能。但是在你的日常工作中,你需要一个额外的参数来表示

的最大尺寸,因为你的stopx变量用来表示

应该填充的块的最大下标

而不是数组可能的最大下标。

另一种主要的多维数组是每个

级别的数组 - 除了 - 最后一个是指向下一个较低维度的对象的指针的矢量(一维

数组), />
直到最后,倒数第二个维度是指针的向量

到值。在参数列表中声明这样一个对象

的方法不止一种;有些人会使用,例如,

int **数组

在代码中使用这个数组看起来仍然完全相同,

数组[ i] [j]

但这意味着与第一个

类型数组完全不同:它意味着从
命名数组的位置的开头,那个位置

应该用作指向内存块的指针,你可以用它来b
采取的第j个元素。当你使用这种

多维数组时,没有必要提前知道每个维度的最大尺寸 -

-

但它也意味着用于存储数组

值的实际内存 - 可能 - 不是一个普通的存储块(因为指针

可能在内存中占据一席之地。)

-

没有人有权通过
$ b $摧毁另一个人的信念b要求经验证据。 - Ann Landers
In article <29************@news.tdc.dk>,
=?ISO-8859-1?Q?Martin_J=F8rgensen?= <un*********@spam.jay.net> wrote:

int array[][]

You cannot declare an array like that in C.

There are different kinds of multi-dimensional arrays in C90.

One of them is a block of consequative memory addresses. In order
for C to be able to find the proper location in that block, it
needs to know what maximum size was for all of the dimensions except
the last, as it needs to do the equivilent of
(i-1)*maxfirstdim + (j-1) in order to find the offset from the
beginning of the block of memory. But C doesn''t record that dimensional
information as a property of pointers that get passed around, so
when you pass one of these kinds of arrays into another routine,
that other routine has to have a declaration of the array size.

In C90, it is not possible to pass in the size of a multidimensional
array and have a declaration that varies with the passed size.

In C99, the capability to do that was added. But in the context
of your routine, you would need an additional parameter to indicate
the maximum dimension, as your stopx variable is used to indicate
the maximum subscript of the the block that should be filled
rather than the maximum subscript that is possible for the array.
The other major kind of multi-dimensional array is to have each
level of the array -except- the last be a vector (one-dimensional
array) of pointers to objects of the next lower dimensionality,
until finally the second-last dimension is a vector of pointers
to values. There is more than one way to declare such an object
within a parameter list; some people would use, e.g.,
int **array
Using this array in the code would still look exactly the same,
array[i][j]
but it would mean something completely different than in the first
kind of array: it would mean to access the i''th location from
the beginning of the location named array, and that that location
should be used as a pointer to a block of memory, which you
would then take the j''th element of. When you use this kind of
multi-dimensional array, it is not necessary for functions to
know ahead of time what the maximum sizes are for each dimension --
but it also means that the actual memory used to store the array
values is -likely- not a plain block of storage (since pointers
might point all over the place in memory.)
--
"No one has the right to destroy another person''s belief by
demanding empirical evidence." -- Ann Landers


Walter Roberson写道:
Walter Roberson wrote:
文章< 29 ************ @ news .tdc.dk>,
=?ISO-8859-1?Q?Martin_J = F8rgensen?=< un ********* @ spam.jay.net>写道:
-snip-

到值。在参数列表中声明这样的对象有多种方法;有些人会使用,例如,
int ** array
在代码中使用这个数组看起来仍然完全一样,
array [i] [j]
但它会意味着与第一种阵列完全不同的东西:它意味着从名为array的位置的开头访问第i个位置,那个位置应该是用作指向内存块的指针,然后你将采用它的第j个元素。当你使用这种


好​​吧...我是否必须使用malloc或其他东西?因为我还没有真正进入

这个功能......

多维数组,没有必要让功能提前知道每个维度的最大大小是什么 -
但它也意味着用于存储数组值的实际内存 - 可能 - 不是一个普通的存储块(因为指针可能可能)在内存中占据一席之地。)
In article <29************@news.tdc.dk>,
=?ISO-8859-1?Q?Martin_J=F8rgensen?= <un*********@spam.jay.net> wrote: -snip-
to values. There is more than one way to declare such an object
within a parameter list; some people would use, e.g.,
int **array
Using this array in the code would still look exactly the same,
array[i][j]
but it would mean something completely different than in the first
kind of array: it would mean to access the i''th location from
the beginning of the location named array, and that that location
should be used as a pointer to a block of memory, which you
would then take the j''th element of. When you use this kind of
Ok... Do I have to use malloc or something? Because I''m not really into
that function yet...
multi-dimensional array, it is not necessary for functions to
know ahead of time what the maximum sizes are for each dimension --
but it also means that the actual memory used to store the array
values is -likely- not a plain block of storage (since pointers
might point all over the place in memory.)




好​​的......也许有人可以发布一个有效的例子,我可以学习

来自?我明白你写的是什么,但我只是不确定我是否知道

如何编程...


好​​的,我假设在在程序开始时,我会声明:


array ** int


现在,我偷了/借了来自另一个代码的东西 - 也许我可以使用

这个(我甚至不知道这是否有必要?):


doublearray =(double * *)malloc((size_t)2 *(sizeof(double *)));


for(i = 0; i< 2; i ++)

{

doublearray [i] =(double *)malloc((size_t)1700 *(sizeof(double)));

}


现在我也将使用我的fillvalues函数整数我

假设我只是替换单词double在上面用int表示。我知道

以上是在1700 * 2阵列上工作的,所以如果我将它修改为

10 * 10那么我想我只是改变了数字2-> ; 10和1700-> 10?

最好的问候/ Med venlig hilsen

Martin J?rgensen


-

------------------------------------------- --------------------------------

Martin J?rgensen的家 - http://www.martinjoergensen.dk




" Martin J?rgensen" <未********* @ spam.jay.net>在消息中写道

news:dh ************ @ news.tdc.dk ...

"Martin J?rgensen" <un*********@spam.jay.net> wrote in message
news:dh************@news.tdc.dk...
Walter Roberson写道:
Walter Roberson wrote:
文章< 29 ************ @ news.tdc.dk>,
=?ISO-8859-1?Q?Martin_J = F8rgensen?=< ;未********* @ spam.jay.net>写道:
In article <29************@news.tdc.dk>,
=?ISO-8859-1?Q?Martin_J=F8rgensen?= <un*********@spam.jay.net> wrote:


-snip-


-snip-

到值。在参数列表中声明这样的对象有多种方法;有些人会使用,例如,
int ** array
在代码中使用这个数组看起来仍然完全一样,
array [i] [j]
但它会意味着与第一种阵列完全不同的东西:它意味着从名为array的位置的开头访问第i个位置,那个位置应该是用作指向内存块的指针,然后你将采用它的第j个元素。当你使用这种
to values. There is more than one way to declare such an object
within a parameter list; some people would use, e.g.,
int **array
Using this array in the code would still look exactly the same,
array[i][j]
but it would mean something completely different than in the first
kind of array: it would mean to access the i''th location from
the beginning of the location named array, and that that location
should be used as a pointer to a block of memory, which you
would then take the j''th element of. When you use this kind of



好的...我是否必须使用malloc或其他东西?因为我还没有真正进入那个功能......



Ok... Do I have to use malloc or something? Because I''m not really into
that function yet...

多维数组,没有必要让功能提前知道每个维度的最大大小是什么 -
但它也意味着用于存储数组值的实际内存 - 可能 - 不是一个普通的存储块(因为指针可能可能)在内存中占据一席之地。)
multi-dimensional array, it is not necessary for functions to
know ahead of time what the maximum sizes are for each dimension --
but it also means that the actual memory used to store the array
values is -likely- not a plain block of storage (since pointers
might point all over the place in memory.)



好的......也许有人可以发布一个有效的例子,我可以从中学到什么?我明白你写的是什么,但我只是不确定我是否知道如何编程它...

好的,我假设在程序开始时,我会宣布:

array ** int

现在,我偷了/借了来自另一个代码的东西 - 也许我可以使用
这个(我甚至不知道这是否有必要?):

doublearray =(double **)malloc((size_t)2 *(sizeof(double *)));

for(i = 0; i< 2; i ++)
{
doublearray [i] =(double *)malloc( (size_t)1700 *(sizeof(double)));
}

现在我也将使用我的fillvalues整数上的函数我假设我只是将单词double替换为double。在上面用int表示。我知道
以上是在1700 * 2阵列上工作的,所以如果我将其修改为10 * 10那么我想我只需更改数字2> 10和1700-> 10 ?



Ok... Perhaps somebody could post a working example, that I could learn
from? I understand what you write, but I''m just not sure if I know how
to program it...

Okay, I assume in the beginning of the program, I would declare:

array** int

Now, I "stole/borrowed" something from another code - perhaps I can use
this (I don''t even know if this is necessary?):

doublearray = (double**) malloc((size_t) 2*(sizeof(double*)));

for(i=0;i<2;i++)
{
doublearray[i] = (double*) malloc((size_t) 1700*(sizeof(double)));
}

Now since I will also be using my "fillvalues" function on integers I
assume I just replace the word "double" in the above with "int". I know
that the above works on a 1700*2 array, so if I should modify it to
10*10 then I guess I just change the number 2->10 and 1700->10?



关于原始代码,将(nx + 1)和1700替换为(ny + 1)。

你有这里无关紧要的并发症因为下标的变化从1到0,你可以放弃演员。

当行是不同长度的字符串时,该方案最有用,

,因为你可以分别设置每一行的长度。对于统一行,这是一个更简单的C90

版本:


/ * prototype * /

void fillinnumbers(int,int, int,int,int,int **);


/ * definition * /

void fillinnumbers(int fillvalue,int startx,int stopx,int starty ,int

stopy,int ** array)

{

for(i = startx; i< = stopx; i ++)

for(j = starty; j< = stopy; j ++)

array [i] [j] = fillvalue;

}


/ *调用者* /

int ** array = malloc((nx + 1)* sizeof(int *));

array [ 0] = malloc((nx + 1)*(ny + 1)* sizeof(int));

for(i = 1; i< nx + 1; i ++)

array [i] = array [i-1] + ny + 1; / *保存每行的起始地址* /

fillinnumbers(3,1,nx,1,ny,array);

free(array [0]);

免费(数组);


或者,您可以根据需要动态执行下标算术,而不是

存储行地址。想想行。作为一个

长的一维数组的片段。请注意,您需要将宽度传递到

,无论何处需要下标算术


/ * prototype * /

void fillinnumbers (int,int,int,int,int,int,int *);


/ * definition * /

void fillinnumbers(int fillvalue,int startx ,int stopx,int starty,int

stopy,int ncols,int * a)

{

for(i = startx; i< = stopx; i ++)

{

int * array_i = a + i * ncols;

for(j = starty; j< = stopy ; j ++)

array_i [j] = fillvalue;

}

}


/ *调用者* /

int * p = malloc((nx + 1)*(ny + 1)* sizeof(int));

fillinnumbers(3,1,nx ,1,ny,ny + 1,p);

免费(p);


但你不需要做任何这些。您的编译器具有C99的可变长度

阵列支持。这一定是每个人最喜欢的改进,但是如果每个人都坚持让他们的

代码可以永久地移植到C90,那么优势永远不会实现。


在C99中,您只需要一个额外的参数和一些巧妙的语法:


/ * prototype * /

void fillinnumbers(int,int,int,int,int,int,int array [] [*]);


/ * definition * /

void fillinnumbers(int fillvalue,int startx,int stopx,int starty,int

stopy,int ncols,int array [] [ncols])

{

for(i = startx; i< = stopx; i ++)

for(j = starty; j< = stopy; j ++)

array [i] [j] = fillvalue;

}


/ * caller * /

int array [nx + 1] [ny + 1];

fillinnumbers(3,1,nx,1,ny,ny + 1,数组);


In relation to your original code, replace 2 by (nx+1) and 1700 by (ny+1).
You''ve got irrelevant complications here because of the change of subscript
base from 1 to 0. You can drop the casts.
That scheme is most useful when the rows are strings of varying length,
since you can set the length of each row separately. This is a simpler C90
version for uniform rows:

/* prototype */
void fillinnumbers(int, int, int, int, int, int **);

/* definition */
void fillinnumbers(int fillvalue, int startx, int stopx, int starty, int
stopy, int **array)
{
for(i=startx; i<=stopx; i++)
for(j=starty; j<=stopy; j++)
array[i][j] = fillvalue;
}

/* caller */
int **array = malloc((nx+1)*sizeof(int*));
array[0] = malloc((nx+1)*(ny+1)*sizeof(int));
for (i = 1; i < nx+1 ;i++)
array[i] = array[i-1] + ny+1; /* save start address of each row */
fillinnumbers(3, 1, nx, 1, ny, array);
free(array[0]);
free(array);

Or, you can just do the subscript arithmetic on the fly as required, instead
of storing the row addresses. Think of the "rows" as being segments of a
long one-dimensional array. Note that you have to pass the width around to
wherever subscript arithmetic is needed

/* prototype */
void fillinnumbers(int, int, int, int, int, int, int *);

/* definition */
void fillinnumbers(int fillvalue, int startx, int stopx, int starty, int
stopy, int ncols, int *a)
{
for(i=startx; i<=stopx; i++)
{
int *array_i = a + i*ncols;
for(j=starty; j<=stopy; j++)
array_i[j] = fillvalue;
}
}

/* caller */
int *p = malloc((nx+1)*(ny+1)*sizeof(int));
fillinnumbers(3, 1, nx, 1, ny, ny+1, p);
free(p);

But you don''t need to do any of that. Your compiler has the variable-length
array support from C99. This must be everybody''s favourite improvement, but
the advantage will never be realised if everybody insists on making their
code back-portable to C90 for ever.

In C99 all you need is an extra parameter and a bit of clever syntax:

/* prototype */
void fillinnumbers(int, int, int, int, int, int, int array[][*]);

/* definition */
void fillinnumbers(int fillvalue, int startx, int stopx, int starty, int
stopy, int ncols, int array[][ncols])
{
for(i=startx; i<=stopx; i++)
for(j=starty; j<=stopy; j++)
array[i][j] = fillvalue;
}

/* caller */
int array[nx+1][ny+1];
fillinnumbers(3, 1, nx, 1, ny, ny+1, array);


这篇关于试图让我的“填充值”功能工作......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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