初始化指针数组 [英] Intializing Pointer Array

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

问题描述

讨论参考以下代码:


#include< stdio.h>

#include< stdlib.h>


#define MAXCOLS 10

int main()


{


int(* a)[MAXCOLS];

int nrows,ncols,i;


scanf("%d"& nrows) ;

scanf("%d"& ncols);


/ *分配初始内存* /


* a =(int *)malloc(sizeof(int));

/ *代码续* /


}


编译此代码时,它会给出错误,例如

不兼容的类型


这可能是rvalue返回int *

但是左值正在期待int []。


我尝试了不同的选项,但其中一些正在编译

在某些编译器中发出警告而在某些编译器中发出错误。


任何人都可以告诉我如何解决这个问题。

The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];
int nrows,ncols,i;

scanf("%d",&nrows);
scanf("%d",&ncols);

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));
/* Code continued */

}

While compiling this code it is giving error like
"Incompatible types in assingment"

This is probably the rvalue is returning int *
but the lvalue is expecting int [].

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.

推荐答案

**** ***@gmail.com 说:

讨论参考以下代码:


#include< stdio.h>

#include< stdlib.h>


#define MAXCOLS 10

int main()


{


int(* a)[MAXCOLS];
The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];



a是指向MAXCOLS整数数组的指针。

a is a pointer to an array of MAXCOLS ints.


int nrows,ncols,i;


scanf("%d"& nrows);
int nrows,ncols,i;

scanf("%d",&nrows);



这有用吗?你怎么知道的?

Did this work? How do you know?


scanf("%d",& ncols);
scanf("%d",&ncols);



那有用吗?你怎么知道的?

Did that work? How do you know?


>

/ *分配初始内存* /


* a =(int *)malloc(sizeof(int));
>
/* allocate intial memory */

*a = (int *) malloc( sizeof(int));



你只为一个int分配足够的空间,这可能不是你想要的
。你正在制作一个完全逃脱我的角色。

你试图为一个指向的对象赋值,

不幸的是,实际上并没有指出任何对象。即使它确实如此,它也会指向一个数组对象,并且你不能分配给数组

对象。

You''re only allocating enough space for a single int, which probably isn''t
what you want. You''re making a cast the point of which escapes me entirely.
And you''re trying to assign a value to the object pointed to by a which,
unfortunately, doesn''t actually point at any object. And even if it did, it
would be pointing to an array object, and you can''t assign to array
objects.


在编译此代码时,它会给出错误,例如

"Assingment中的不兼容类型
While compiling this code it is giving error like
"Incompatible types in assingment"



肯定不是吗? :-)

Surely not? :-)


这可能是rvalue返回int *

但是左值正在期待int []。
This is probably the rvalue is returning int *
but the lvalue is expecting int [].



你不能分配给数组,记住。

And you can''t assign to arrays, remember.


>

我尝试了不同的选项,但其中一些正在编译

并在某些编译器中发出警告,而在某些编译器中却出错。


可以任何人请让我知道如何纠正这一点。
>
I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.



而不是试图变得聪明,简单地说明你想要实现的目标,也许我们可以想出一个更好的方法来实现它。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

Instead of trying to be clever, state in simple terms what you are trying to
achieve, and maybe we can come up with a better way for you to achieve it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


在文章< 11 ******* **************@79g2000cws.googlegroups.c om>,
至* ******@gmail.com 说...
In article <11*********************@79g2000cws.googlegroups.c om>,
to*******@gmail.com says...

讨论参考以下代码:


#include< stdio.h>

#include< stdlib.h>


#define MAXCOLS 10

int main()


{


int(* a)[MAXCOLS];

int nrows,ncols,i;


scanf("%d"& nrows);

scanf("%d",& ncols);


/ *分配初始内存* /

* a =(int *)malloc(sizeof(int));


/ *代码续* /


}


编译此代码时,它给错误或者像

不兼容的分配类型


这可能是rvalue返回int *

但是左值是期待int []。


我尝试了不同的选项,但其中一些正在编译

并在某些编译器中发出警告,而在某些编译器中却出现错误。


任何人都可以告诉我如何解决这个问题。

The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];
int nrows,ncols,i;

scanf("%d",&nrows);
scanf("%d",&ncols);

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));
/* Code continued */

}

While compiling this code it is giving error like
"Incompatible types in assingment"

This is probably the rvalue is returning int *
but the lvalue is expecting int [].

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.

http://web.torek.net/torek/c/pa.html
http://pw1.netcom.com/~tjensen/ptr/pointers.htm




Richard Heathfield写道:

Richard Heathfield wrote:
到******* @ gmail.com 说:

讨论在使用以下代码参考:


#包括< stdio.h>

#include< stdlib.h>


#define MAXCOLS 10

int main( )


{


int(* a)[MAXCOLS];
The discussion is in reference with the following code :

#include <stdio.h>
#include <stdlib.h>

#define MAXCOLS 10
int main()

{

int (*a)[MAXCOLS];



a是指向MAXCOLS整数数组的指针。


a is a pointer to an array of MAXCOLS ints.


int nrows,ncols,i;


scanf("%d"& nrows);
int nrows,ncols,i;

scanf("%d",&nrows);



这有用吗?你怎么知道的?


Did this work? How do you know?


scanf("%d",& ncols);
scanf("%d",&ncols);



那有用吗?你怎么知道的?


Did that work? How do you know?



/ *分配初始内存* /


* a =(int * )malloc(sizeof(int));

/* allocate intial memory */

*a = (int *) malloc( sizeof(int));



你只为一个int分配足够的空间,这可能不是你想要的b $ b。你正在制作一个完全逃脱我的角色。

你试图为一个指向的对象赋值,

不幸的是,实际上并没有指出任何对象。即使它确实如此,它也会指向一个数组对象,并且你不能分配给数组

对象。


You''re only allocating enough space for a single int, which probably isn''t
what you want. You''re making a cast the point of which escapes me entirely.
And you''re trying to assign a value to the object pointed to by a which,
unfortunately, doesn''t actually point at any object. And even if it did, it
would be pointing to an array object, and you can''t assign to array
objects.


在编译此代码时,它会给出错误,例如

"Assingment中的不兼容类型
While compiling this code it is giving error like
"Incompatible types in assingment"



肯定不是吗? :-)


Surely not? :-)


这可能是rvalue返回int *

但是左值正在期待int []。
This is probably the rvalue is returning int *
but the lvalue is expecting int [].



你不能分配给阵列,请记住。


And you can''t assign to arrays, remember.



我有尝试了不同的选项,但其中一些正在编译

并在某些编译器中发出警告,而在某些编译器中却出错。


任何人都可以告诉我如何纠正这一点。

I have tried different options but some of them are compiling
with a warning in some compilers while in some it is giving error.

Can any one please let me know how to rectify this.



而不是试图变得聪明,简单地说明你想要实现的目标,也许我们可以想出一个更好的方法来实现它。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


Instead of trying to be clever, state in simple terms what you are trying to
achieve, and maybe we can come up with a better way for you to achieve it.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.



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

是的你是对的


我们不能分配给数组对象。


实际上我正在执行一本用书写的程序。

在那本书中,语句类似于

//(MAXCOLS = 30 )


int(* a)[MAXCOLS];


* a =(int *)malloc(nrows * ncols * sizeof(int ));


它试图为2D阵列分配内存。


作者给出了解释为


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

a - | | | | | | | |

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

^

|


*(a)


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

(a + 1) - | | | | | | | |

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


^

|

*(a + 1)

所以通过上面提到的命令,作者想要初始化



数组并将malloc返回的起始位置赋予


ie - * a =(int *)malloc( ......)


但不知怎的,程序看起来并不干净清晰。我会尝试

抓住一些相关的链接

给它,并会提出一些解决方案。


感谢您提供的信息。

-------------------------------------------------------------------------------------------------
Yes you are right

We cann''t assign to array objects.

Actually I was executing a program written in a book.
In that book the statement was something like
// (MAXCOLS = 30)

int (*a)[MAXCOLS];

*a = ( int *) malloc( nrows * ncols * sizeof(int));

It was trying to allocate memory for a 2D array.

Author has given the explanation as

-----------------------------------
a - | | | | | | | |
-----------------------------------
^
|

*(a)

-----------------------------------
( a + 1) - | | | | | | | |
-----------------------------------

^
|
*(a + 1)
So by the above mentioned command probably author wants to initialize
the
array and give the starting location returned by malloc to a

i.e. - *a = (int *) malloc(...)

But somehow the program doesn''t look clean and clear.I will try to
catch some links related
to it and will come up with some solution.

Thanks for the information.


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

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