关于指针和多维数组的问题 [英] Question about pointers and multi-dimension arrays

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

问题描述

大家好,圣诞快乐!


我对以下程序有一些疑问:


arrtest.c

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


#include< stdio.h>


int main( int agc,char * argv []){

int array2d [2] [2] = {{1,2},{3,4}};

int (* arrp)[2] = array2d;


printf("& array2d =%p\tarray2d =%p \ t * array2d =%p \ n" ,& array2d,

array2d,* array2d);

printf("& arrp =%p\tarp =%p \ t * arrp =% p\t ** arp =%d \ n",& arrp,

arrp,* arrp,** arrp);


返回0;

}


当我编译并运行程序时,我得到以下结果:


& array2d = 0xbfd9e880 array2d = 0xbfd9e880 * array2d = 0xbfd9e880

& arrp = 0xbfd9e890 arrp = 0xbfd9e880 * arrp =

0xbfd9e880 ** arrp = 1

我的问题是:


为什么array2d(& array2d)的地址,值(array2d)和该值(* array2d)的

内容都是一样的? (0xbfd9e880)


array2d包含值:0xbfd9e880。当解除引用(* array2d)

时它给出相同的值,0xbfd9e880

但是当再次取消引用相同的地址时(** array2d)它会给出

是整数(1)而不是上面的地址。


编译器如何理解如果它应该返回一个指针或

存储在该地址中的值?


编译器如何存储数组变量以便& array == array?


感谢您的时间,我希望您能理解我的问题

Hello everyone, merry christmas!

I have some questions about the following program:

arrtest.c
------------

#include <stdio.h>

int main(int agc, char *argv[]) {
int array2d[2][2] = { {1,2} , {3,4} };
int (* arrp)[2] = array2d;

printf("&array2d = %p\tarray2d = %p\t*array2d = %p\n", &array2d,
array2d, *array2d);
printf("&arrp = %p\tarp = %p\t*arrp = %p\t**arp = %d\n", &arrp,
arrp, *arrp, **arrp);

return 0;
}

When I compile and run the program I get the following results:

&array2d = 0xbfd9e880 array2d = 0xbfd9e880 *array2d = 0xbfd9e880
&arrp = 0xbfd9e890 arrp = 0xbfd9e880 *arrp =
0xbfd9e880 **arrp = 1

My questions are:

Why is the address of array2d (&array2d), the value (array2d) and the
contents of that value (*array2d) all the same ? (0xbfd9e880)

array2d contains the value: 0xbfd9e880. When dereferenced (*array2d)
it gives the same value, 0xbfd9e880
But when the same address is dereferenced again (**array2d) it gives
an integer (1) and not an address like above.

How does the compiler "understands" if it should return a pointer or
the value stored in that address?

How does the compiler stores array variables so that &array == array ?

Thanks for your time, I hope you will understand my questions

推荐答案

DiAvOl说:


< snip>
DiAvOl said:

<snip>

int array2d [2] [2] = {{1,2},{3,4 }};
int array2d[2][2] = { {1,2} , {3,4} };



< snip>

<snip>


为什么是array2d(& array2d)的地址,值(array2d)和那个值(* array2d)的

内容都一样吗?
Why is the address of array2d (&array2d), the value (array2d) and the
contents of that value (*array2d) all the same ?



它们不一样,因为它们有不同的类型。 & array2d的类型为

int(*)[2] [2],array2d的类型为int [2] [2](转换为值

of在值上下文中使用时输入int(*)[2],并且* array2d具有类型

int [2](在值

上下文)。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

They aren''t the same, because they have different types. &array2d has type
int (*)[2][2], array2d has type int[2][2] (which is converted into a value
of type int(*)[2] when used in value contexts), and *array2d has type
int[2] (which is converted into a value of type int * when used in value
contexts).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" DiAvOl" < ch ****** @ yahoo.com在消息新闻中写道:06ce14cb-

"DiAvOl" <ch******@yahoo.comwrote in message news:06ce14cb-

int main(int agc,char * argv []){

int array2d [2] [2] = {{1,2},{3,4}};

int(* arrp)[2] = array2d;


printf("& array2d =%p\tarray2d =%p \ t * array2d =%p \ n",& array2d,

array2d,* array2d);

printf(& arrp =%p\tarp =%p \ t * arrp =%p \ t ** arp =%d \ n",& arrp,

arrp,* arrp,** arrp);


返回0;

}
int main(int agc, char *argv[]) {
int array2d[2][2] = { {1,2} , {3,4} };
int (* arrp)[2] = array2d;

printf("&array2d = %p\tarray2d = %p\t*array2d = %p\n", &array2d,
array2d, *array2d);
printf("&arrp = %p\tarp = %p\t*arrp = %p\t**arp = %d\n", &arrp,
arrp, *arrp, **arrp);

return 0;
}



关于C 89中多维数组的重要事项是

它们被有效地破坏了。虽然它们可以被声明和使用,但是对它们做任何事情所需要的

语法,比如将它们传递给其他

函数,太复杂了。它们也无法在运行时调整大小。


所以只要忘记它们,直到你熟悉其余的大部分

语言。任何多维数组都可以用

单维度轻松模拟。


-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk / ~bgy1mm

The important thing to know about multidimensional arrays in C 89 is that
they are effectively broken. Whilst they can be declared and used, the
syntax needed to do anything much with them, like pass them to other
functions, is too complicated. They also cannot be resized at runtime.

So just forget about them until you are familiar with most of the rest of
the language. Any multi-dimensional array can easily be emulated with a
single dimension.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


0xbfd9e880< ===内存地址

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

| 1 | < ===目录

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


我的观点是,地址0xbfd9e880如何同时拥有地址和

号码?


当array2d被解除引用(* array2d)时,它应该返回内容

'' s值(内存位置0xbfd9e880的内容),但它返回

相同的地址。


当array2d被双重引用时(** array2d)它是'与我们之前看到的

*(* array2d),* array2d相同的是地址0xbfd9e880所以

这次取消引用它会返回一个整数。 />

编译器如何确定何时返回一个整数以及何时返回

返回地址?


感谢您的支持快速回复
0xbfd9e880 <=== Memory address
------------
| 1 | <=== Contents
------------

My point is, how can the address 0xbfd9e880 hold both an address and a
number ?

When array2d is dereferenced (*array2d) it should return the contents
of it''s value (contents of memory location 0xbfd9e880) but it returns
the same address.

When array2d is double dereferenced (**array2d) it''s the same as
*(*array2d), *array2d as we saw before is the address 0xbfd9e880 so
when it is dereferenced this time, it returns an integer.

How can the compiler determine when to return an integer and when to
return the address?

Thanks for your fast reply


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

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