使用指针进行多维数组初始化 [英] multi dimension array initialization using pointers
问题描述
我正试图理解指针的使用(至少掌握指针是如何工作的)。我已经阅读了关于
的常见问题解答 http ://www.eskimo.com/~scs/C-faq/s6.html 关于指针和数组,但是我还是有点丢失。
>
我写下面的代码试图理解它,但它不是
工作:
#include< stdio .h>
#include< ctype.h>
#define MAXROW 2
#define MAXCOL 5 >
void init_array(char * data [MAXROW]);
void print_array(char * data [MAXROW]);
int main( )
{
char * array_ptr [MAXROW];
init_array(array_ptr);
print_array(array_ptr);
返回0;
}
void init_array(char * data [MAXROW])
{
int row,col;
for(row = 0; row< MAXROW; row ++)
{
for(col = 0; col< MAXCOL; col ++)
{
(data [row] + col)=''\ 0'';
}
}
}
void print_array(char * data [MAXROW])
{
int row,col;
for(row = 0;行< MaxRow的;行++)
{
for(col = 0; col< MAXCOL; col ++)
{
printf( "%c",*(data [row] + col));
}
}
printf(" \\\
" ;);
}
当我尝试用gcc编译它时,有一个警告
a3.c:In函数`init_array'':
a3.c:29:赋值中左值无效
有人能告诉我警告的含义吗?
我做错了什么?
Nat
Hi,
I''m attempting to understand the use of pointers(at least grasp how
pointers work). I''ve read the FAQ on
http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I''m
still a bit lost.
I written the following code to try to understand it but it''s not
working:
#include <stdio.h>
#include <ctype.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char *data[MAXROW]);
void print_array(char *data[MAXROW]);
int main ()
{
char *array_ptr[MAXROW];
init_array(array_ptr);
print_array(array_ptr);
return 0;
}
void init_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
(data[row] + col) = ''\0'';
}
}
}
void print_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
printf("%c",*(data[row] + col));
}
}
printf("\n");
}
When I try to compile it with gcc, there is a warning
a3.c: In function `init_array'':
a3.c:29: invalid lvalue in assignment
Can anyone tell me what the warning means ?
What I am doing wrong ?
Nat
推荐答案
na **** @ yahoo.com.au 写道:
我正在尝试理解指针的使用(至少掌握指针是如何工作的)。我已经阅读了关于
的常见问题解答 http ://www.eskimo.com/~scs/C-faq/s6.html 关于指针和数组但是我还是有点迷失。
我写下面的代码试图理解它,但它没有工作:
#include< stdio.h>
#include< ctype.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char * data [MAXROW]);
void print_array(char * data [MAXROW] ]);
int main()
{* char * array_ptr [MAXROW];
init_array(array_ptr);
print_array( array_ptr);
返回0;
}
void init_array(char * data [MAXROW])
{
int row,col;
for(row = 0; row< MAXROW; row ++)
{
for(col = 0; col< MAXCOL; col ++)
{
(data [data行] + col)=''\ 0'';
}
}
}
int_array(char * data [MAXROW])
{int row,col;
for(row = 0;行< MaxRow的;行++)
{
for(col = 0; col< MAXCOL; col ++)
{
printf("%c",*(data [row] + col)) ;
}
}
printf(" \ n");
}
当我尝试用gcc编译它时,有一个警告
a3.c:在函数`init_array'':
a3.c:29:赋值中左值无效
谁能告诉我警告的含义是什么?>我做错了什么?
Hi,
I''m attempting to understand the use of pointers(at least grasp how
pointers work). I''ve read the FAQ on
http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I''m
still a bit lost.
I written the following code to try to understand it but it''s not
working:
#include <stdio.h>
#include <ctype.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char *data[MAXROW]);
void print_array(char *data[MAXROW]);
int main ()
{
char *array_ptr[MAXROW];
init_array(array_ptr);
print_array(array_ptr);
return 0;
}
void init_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
(data[row] + col) = ''\0'';
}
}
}
void print_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
printf("%c",*(data[row] + col));
}
}
printf("\n");
}
When I try to compile it with gcc, there is a warning
a3.c: In function `init_array'':
a3.c:29: invalid lvalue in assignment
Can anyone tell me what the warning means ?
What I am doing wrong ?
/ * BEGIN new.c * /
#include< stdio.h> ;
#include< assert.h>
#include< stdlib.h>
#include< string.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char ** data);
void print_array(char ** data);
void free_array(char ** data);
int main(void)
{
char * array_ptr [MAXROW];
init_array(array_ptr);
print_array( array_ptr); <无线电通信/>
free_array(array_ptr);
返回0;
}
void init_array(char ** data )
{
int row;
for(row = 0;行< MaxRow的;行++){
data [row] = malloc(MAXCOL);
if(data [row] == NULL){
puts( " malloc");
退出(EXIT_FAILURE);
}
断言(MAXCOL> = sizeof" data");
strcpy(数据[行],数据);
}
}
void print_array(char ** data)
{
int row;
for(row = 0; row< MAXROW ;行++){
fputs(data [row],stdout);
}
putchar(''\ n'');
}
void free_array(char ** data)
{
int row;
for(row = 0; row< MAXROW; row ++){
free(data [row]);
}
}
/ * END new.c * /
-
pete
/* BEGIN new.c */
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#define MAXROW 2
#define MAXCOL 5
void init_array(char **data);
void print_array(char **data);
void free_array(char **data);
int main (void)
{
char *array_ptr[MAXROW];
init_array(array_ptr);
print_array(array_ptr);
free_array(array_ptr);
return 0;
}
void init_array(char **data)
{
int row;
for (row = 0; row < MAXROW; row++) {
data[row] = malloc(MAXCOL);
if (data[row] == NULL) {
puts("malloc");
exit(EXIT_FAILURE);
}
assert(MAXCOL >= sizeof "data");
strcpy(data[row], "data");
}
}
void print_array(char **data)
{
int row;
for (row = 0; row < MAXROW; row++) {
fputs(data[row], stdout);
}
putchar(''\n'');
}
void free_array(char **data)
{
int row;
for (row = 0; row < MAXROW; row++) {
free(data[row]);
}
}
/* END new.c */
--
pete
2005年10月8日00:02:35 -0700
na * ***@yahoo.com.au 写道:
On 8 Oct 2005 00:02:35 -0700
na****@yahoo.com.au wrote:
#define MAXROW 2
#defi ne MAXCOL 5
为什么const const MAXROW = 2; ?
void init_array(char * data [MAXROW])
{int row,col;
for(row = 0; row< MAXROW ;行++)
{
for(col = 0; col< MAXCOL; col ++)
{
(data [row] + col)=''\''';
这不行。应该是:
数据[row + col] =''\ 0'';
}
}
}
data'有5(0..4)个char *元素。你试图在这个for循环中访问元素
0..9,这当然不会很好。
在这里相同:
void print_array(char * data [MAXROW])
{row,col;
for(row = 0; row< MAXROW;行++)
{
for(col = 0; col< MAXCOL; col ++)
{
printf("%c",*(data [row] + col)) ;
这不行。应该是:
printf("%c",data [row + col]);
}
}
printf(" \\ \\ n");
}
#define MAXROW 2
#define MAXCOL 5
Why not const int MAXROW = 2; ?
void init_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
(data[row] + col) = ''\0'';
This won''t work. Should be:
data[row + col] = ''\0'';
}
}
}
data''s got 5 (0..4) char* elements. You are trying to access elements
0..9 in this for loop, which of course won''t work out well.
Same in here:
void print_array(char *data[MAXROW])
{
int row, col;
for (row=0; row<MAXROW; row++)
{
for (col=0; col<MAXCOL; col++)
{
printf("%c",*(data[row] + col));
This won''t work. Should be:
printf("%c",data[row + col]);
}
}
printf("\n");
}
将数组声明更改为char * array_ptr [MAXROW * MAXCOL];
或退出第二个for循环。
BTW:void print_array(char * data [MAXROW])风格非常糟糕。你应该提供数组中元素的数量作为第二个
参数:void print_array(char ** data,int elements)
使用print_array调用(数据,MAXCOL * MAXROW);
最好的问候/ Gru?
Moritz Beller
-
web http://www.4momo.de >
邮件momo dot beller at t-online dot de
gpg http://gpg.notlong.com
Moritz Beller写道:
Moritz Beller wrote:
2005年10月8日00:02:35 - 0700
na****@yahoo.com.au 写道:
On 8 Oct 2005 00:02:35 -0700
na****@yahoo.com.au wrote:
#define MAXROW 2
#define MAXCOL 5
#define MAXROW 2
#define MAXCOL 5
为什么const int MAXROW = 2; ?
Why not const int MAXROW = 2; ?
因为在C中,const不生成编译时常量。
所以,对于C99之前的C,你不能用它来做数组大小为
声明。或案例标签。
你说的是另一种语言,也许是C ++。
干杯
Michael
-
电子邮件:我的是/ at / gmx / dot / de地址。
Because in C, const does not generate compile time constants.
So, for pre-C99 C, you cannot use it for array sizes at
declaration. Or case labels.
You are talking about another language, maybe C++.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
这篇关于使用指针进行多维数组初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!