多维数组初始化 [英] multi dimension array initialization

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

问题描述



我是C的新手,所以希望有人可以给我一些关于

我出错的指导。


我写了下面的代码,试图初始化多暗的

数组,但它不起作用:


#include< stdio.h>

#include< ctype.h>


#define ROW 2

#define COL 5

void init_array();


main()

{

char数组[行] [COL];


init_array(数组);

返回0;

}


void init_array(char data [] [])

{

int r,c;

for(r = 0; r< = ROW; r ++)

{

for(c = 0; c< = COL; c ++)

{

data [r] [c] = NULL;

}

}

}


但是当我尝试用gcc编译时,我收到了这条消息

a2.c:在函数`init_array'':

a2.c:28:算术开启指向不完整类型的指针


任何人都可以用英语解释错误的含义吗?

我做错了什么?


Nat


我已经尝试过发布到comp.lang.c.moderated但是我必须做完

因为它没有''尚未发布。我的意见如果它是
出现两次。

Hi,
I''m new to C so hopefully someone can give me some guidance on where
I''ve gone wrong.

I''ve written the following code, trying to initialize the multi-dim
array but it''s not working:

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();

main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL;
}
}
}

But when I try to compile with gcc, I''m getting this message
a2.c: In function `init_array'':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?
What am I doing wrong ?

Nat

I''ve tried posting to comp.lang.c.moderated but I must have done
something incorrect as it hasn''t been posted yet. My appologies if it
appears twice.

推荐答案

na **** @ yahoo.com.au 于17/09/05写道:
na****@yahoo.com.au wrote on 17/09/05 :
#include< stdio.h>
#include< ctype.h>

#define ROW 2
#define COL 5

void init_array();


非原型(IOW,无用的声明。)

main()
{char / array [ROW] [ COL];

init_array(数组);
返回0;
}

void init_array(char data [] [])


所需尺寸(最左边可以省略)。最好也传递

尺寸。 (或使用指向数组的指针)。

{
int r,c;

for(r = 0; r< = ROW; r ++)


你的限制是1.

{
for(c = 0; c< = COL; c ++)


同上。

{
data [r] [c] = NULL;


NULL用于指针。使用0作为int。

}
}
}
#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();
Not-a-prototype (IOW, useless declaration.)
main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
Dimensions required (the leftmost can be ommited). Better to pass the
dimensions too. (Or use a pointer to array).
{
int r, c;

for (r=0; r<=ROW; r++)
You are going ot of the limits by 1.
{
for (c=0; c<=COL; c++)
ditto.
{
data[r][c] = NULL;
NULL is for pointers. Use 0 for an int.
}
}
}




试试吧。询问你不了解的细节。


#include< stdio.h>

#include< ctype.h>


#define ROW 2

#define COL 5

static void init_array(char data [] [COL],size_t row ,size_t col)

{

size_t r;


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

{

size_t c;

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

{

数据[r] [c] = r + c;

}

}

}


static void print_array(char const data [] [COL],size_t row,size_t

col)

{

size_t r;


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

{

size_t c;

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

{

printf("%3d",data [r] [c]);

}

printf(" \ n");

}

}


int main(无效)

{

char数组[行] [COL];

in it_array(array,ROW,COL);

print_array(array,ROW,COL);

返回0;

}


0 1 2 3 4

1 2 3 4 5


-

Emmanuel

C-FAQ: http ://www.eskimo.com/~scs/C-faq/faq.html

C库: http://www.dinkumware.com/refxc.html


我曾经问过专家COBOL程序员,如何
在COBOL中声明局部变量,回复是:

什么是局部变量?



Try that. Ask for details you don''t understand.

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

static void init_array (char data[][COL], size_t row, size_t col)
{
size_t r;

for (r = 0; r < row; r++)
{
size_t c;
for (c = 0; c < col; c++)
{
data[r][c] = r + c;
}
}
}

static void print_array (char const data[][COL], size_t row, size_t
col)
{
size_t r;

for (r = 0; r < row; r++)
{
size_t c;
for (c = 0; c < col; c++)
{
printf ("%3d", data[r][c]);
}
printf ("\n");
}
}

int main (void)
{
char array[ROW][COL];

init_array (array, ROW, COL);
print_array (array, ROW, COL);
return 0;
}

0 1 2 3 4
1 2 3 4 5

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

I once asked an expert COBOL programmer, how to
declare local variables in COBOL, the reply was:
"what is a local variable?"

< br>

na****@yahoo.com.au 写道:

你好,
我是C的新手,所以希望有人可以给我一些关于我出错的地方的指导。

我是我写了下面的代码,试图初始化多朦胧阵列,但它不起作用:

#include< stdio.h>
#include< ctype.h>

#define ROW 2
#define COL 5

void init_array();

main()
{char / array char [行[COL];

init_array(数组);
返回0;
}

void init_array(char data [] [])< (r = 0;对于(r = 0;为r = ROW; r ++)
{
for(c = 0; c< = COL; c ++)
{
data [r] [c] = NULL;
} a2.c:函数`init_array'':
a2.c:28:指向不完整类型的指针的算术

任何人都可以用英语解释错误的含义吗?

Hi,
I''m new to C so hopefully someone can give me some guidance on where
I''ve gone wrong.

I''ve written the following code, trying to initialize the multi-dim
array but it''s not working:

#include <stdio.h>
#include <ctype.h>

#define ROW 2
#define COL 5

void init_array();

main ()
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][])
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL;
}
}
}

But when I try to compile with gcc, I''m getting this message
a2.c: In function `init_array'':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?




我希望我可以。

我不擅长解释。


/ * BEGIN new.c * /


#include< stdio.h>


#define ROW 2

#define COL 5


void init_array(char(* data)[5]);

void print_array(char(* data)[5]);


int main(void )

{

char array [ROW] [COL];


init_array(array);

print_array(array);

返回0;

}


void init_array(char(* data)[5] )

{

int r,c;


for(r = 0; r!= ROW; ++ r){

for(c = 0; c!= COL; ++ c){

data [r] [c] =(char)(r + c);

}

}

}


void print_array(char(* data) )[5])

{

int r,c;


for(r = 0; r!= ROW; ++ r){

for(c = 0; c!= COL; ++ c){

printf("%d",data [r] [c]);

}

}

putchar(''\ n'');

}


/ * END new.c * /

-

pete



I wish I could.
I''m not good at explantions.

/* BEGIN new.c */

#include <stdio.h>

#define ROW 2
#define COL 5

void init_array(char (*data)[5]);
void print_array(char (*data)[5]);

int main (void)
{
char array[ROW][COL];

init_array(array);
print_array(array);
return 0;
}

void init_array(char (*data)[5])
{
int r, c;

for (r = 0; r != ROW; ++r) {
for (c = 0; c != COL; ++c) {
data[r][c] = (char)(r + c);
}
}
}

void print_array(char (*data)[5])
{
int r, c;

for (r = 0; r != ROW; ++r) {
for (c = 0; c != COL; ++c) {
printf("%d ", data[r][c]);
}
}
putchar(''\n'');
}

/* END new.c */
--
pete


na****@yahoo.com.au 写道:


#include< stdio.h>
#include< ctype.h>
您没有使用ctype.h中的任何内容,那么为什么要包含它?

#define ROW 2
#define COL 5

void init_array();
当您声明原型时,请确保您也声明了参数,

提高了可读性。如果我没有错,那么当原型中没有指定时,C编译器会假定

整数参数。

main()
它应该是" int main(void)"

{
char array [ROW] [COL];

init_array(array);
返回0 ;


void init_array(char data [] [])
对于未大小的数组,只能省略最左边的维度。

你必须指明其余部分。因此,将上述内容更正为:

" void init_array(char data [] [COL])"

{
int r,c;

for(r = 0; r< = ROW; r ++)
{
for(c = 0; c< = COL; c ++)
{
数据[r] [c] = NULL;
使用data [r] [c] = 0;。 NULL被定义为指向任何内容的指针,

上面的语句将导致此警告:赋值使得整数来自

指针而不使用强制转换

}
}
}

但是当我尝试用gcc编译时,我收到了这条消息
a2.c:在函数`init_array'中':
a2.c:28:指向不完整类型的指针算术

任何人都可以解释这个错误在英语中意味着什么?
我做错了什么?


我已经尝试过发布到comp.lang.c.moderated但我必须做错了,因为它尚未发布。如果它出现两次我的意见。
Hi,

#include <stdio.h>
#include <ctype.h> You''re not using anything from ctype.h, then why include it?

#define ROW 2
#define COL 5

void init_array(); When you declare a prototype, make sure you declare the parameters too,
improves readability. And if I''m not wrong, then a C compiler assumes
integer parameters when none are specified in the prototype.

main () It should be "int main(void)"
{
char array[ROW][COL];

init_array(array);
return 0;
}

void init_array(char data[][]) For unsized arrays, you can leave out only the leftmost dimension.
You must specify the rest. So correct the above as:
"void init_array(char data[][COL])"
{
int r, c;

for (r=0; r<=ROW; r++)
{
for (c=0; c<=COL; c++)
{
data[r][c] = NULL; Use "data[r][c] = 0;". NULL is defined as a pointer to nothing, the
above statement will cause this warning: "assignment makes integer from
pointer without a cast"
}
}
}

But when I try to compile with gcc, I''m getting this message
a2.c: In function `init_array'':
a2.c:28: arithmetic on pointer to an incomplete type

Can anyone explain what the error means in english ?
What am I doing wrong ?

Nat

I''ve tried posting to comp.lang.c.moderated but I must have done
something incorrect as it hasn''t been posted yet. My appologies if it
appears twice.




欢呼,

forayer


-

如果你真的是一个真正的追求者,那么你生命中至少有一次你必须尽可能地怀疑所有事情。 ;

- Rene Descartes



cheers,
forayer

--
If you would be a real seeker after truth, it is necessary that at least
once in your life you doubt, as far as possible, all things."
-- Rene Descartes


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

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