我的问题是: 为什么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 = 1My questions are:Why is the address of array2d (&array2d), the value (array2d) and thecontents of that value (*array2d) all the same ? (0xbfd9e880)array2d contains the value: 0xbfd9e880. When dereferenced (*array2d)it gives the same value, 0xbfd9e880But when the same address is dereferenced again (**array2d) it givesan integer (1) and not an address like above.How does the compiler "understands" if it should return a pointer orthe 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 thecontents 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 typeint (*)[2][2], array2d has type int[2][2] (which is converted into a valueof type int(*)[2] when used in value contexts), and *array2d has typeint[2] (which is converted into a value of type int * when used in valuecontexts).--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 / ~bgy1mmThe important thing to know about multidimensional arrays in C 89 is thatthey are effectively broken. Whilst they can be declared and used, thesyntax needed to do anything much with them, like pass them to otherfunctions, is too complicated. They also cannot be resized at runtime.So just forget about them until you are familiar with most of the rest ofthe language. Any multi-dimensional array can easily be emulated with asingle 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 anumber ?When array2d is dereferenced (*array2d) it should return the contentsof it''s value (contents of memory location 0xbfd9e880) but it returnsthe same address.When array2d is double dereferenced (**array2d) it''s the same as*(*array2d), *array2d as we saw before is the address 0xbfd9e880 sowhen it is dereferenced this time, it returns an integer.How can the compiler determine when to return an integer and when toreturn the address?Thanks for your fast reply 这篇关于关于指针和多维数组的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-22 14:46