本文介绍了需要帮助声明指向结构数组的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我无法弄清楚如何声明一个指向数组的指针 结构并用值初始化指针。我在这个小组的老帖子上看了一下 ,并尝试了一个看起来很合理的解决方案,但它没有正常工作。这是一个简单的例子,说明我想要完成的事情: //我有一个硬件外围设备,我正试图访问 //有两个端口。每个端口有10个连续的 //寄存器。创建一个结构定义, //定义一个端口。 typedef struct { long long1; long long2; long long3; long long4; long long5; long long6 ; long long7; long long8; long long9; long long10; } volatile my_struct; //创建一个指向两个结构数组的指针 my_struct(* my_struct_ptr)[2]; #define PERIPHERAL_BASEADDR 0xA0000000 int main(int argc,char ** argv) { //这是我用来初始化 //指针的语法。请记住,在这个例子中甚至不需要这行代码 //来说明编译器在寻址方面的行为。 //。 br /> my_struct_ptr =(my_struct(*)[])PERIPHERAL_BASEADDR; printf("& my_struct_ptr [0] =%08X \ n", b $ b& my_struct_ptr [0]); printf("& my_struct_ptr [1] =%08X \ n", & my_struct_ptr [1]); printf(" sizeof(my_struct)=%x \ n", sizeof(my_struct) )); printf(" sizeof(my_struct_ptr [0])=%x \ n", sizeof(my_struct_ptr [0])) ; printf(" sizeof(my_struct_ptr [1])=%x \ n", sizeof(my_struct_ptr [1])); printf("&(my_struct_ptr [0] - > long1)=%08X \ n", &(my_struct_ptr [0] - > long1)); printf("&(my_struct_ptr [0] - > long10)=%08X \ n" , &(my_struct_ptr [0] - > long10)); printf("&(my_struct_ptr [1] - > long1 )=%08X \ n", &(my_struct_ptr [1] - > long1)); printf("&( my_struct_ptr [1] - > long10)=%08X \ n", &(my_struct_ptr [1] - > long10)); } 这是程序的输出: & my_struct_ptr [0] = A0000000 & my_struct_ptr [ 1] = A0000050 sizeof(my_struct)= 28 sizeof(my_struct_ptr [0])= 50 sizeof(my_struct_ptr [1]) = 50 &(my_struct_ptr [0] - > long1)= A0000000 &(my_struct_ptr [0] - > long10)= A0000024 &(my_struct_ptr [1] - > long1)= A0000050 &(my_struct_ptr [1] - > long10)= A0000074 它正确计算了typedef'结构的大小,但是它b / b 报告数组的每个元素的大小是它的两倍大 应该是。如果数组中有三个元素,则每个元素的大小为b / b $ 3倍。在这个例子中,第二个 元素的long1应该在地址A0000028。 我使用的是GCC编译器,但我不认为它是 编译器中的错误。我在Visual C ++中尝试了它并获得了相同的结果。 我尝试使用指向单个结构的指针并将其用作 数组。只有一半有效,因为编译器并没有将它看作是一个结构数组,因此你无法访问 结构和索引的元素。好吧。 任何帮助都将不胜感激。 谢谢, Greg 解决方案 在文章< 11 ********************** @ m79g2000cwm.googlegroups .com> < gc *** @ trancer.comwrote: > typedef struct {...} volatile my_struct; 一般来说,我建议不要制作无名结构类型和 然后给它们别名。混合使用时,我建议更加谨慎。 " volatile"请参阅< http://web.torek.net/torek/c/types2.html>。 (但这些都不是问题。) > //创建指向两个结构数组的指针 my_struct(* my_struct_ptr)[2]; [其余代码剪断] 你声明一个指向数组的指针在这里,正如你的评论 暗示;但是你不希望* a指向数组的指针,你想要 a指向元素的指针。这是因为一个指向T的指针,对于某些类型为T的b / b,通常 - 甚至可能是通常,并且当然在你的情况下为 - 点对于T型的许多元素中的第一个,那个 在一个数组中。 指向T的阵列N的指针。指向许多数组数组中的第一个 数组。在这种情况下,你指向 指针指向第一个T的数组2,所以p [i]是 我是' ; T的数组2。见< http://web.torek.net/torek/c/pa.html>。 - In-Real-Life:Chris Torek,Wind River Systems 美国犹他州盐湖城(40°39.22''N,111°50.29''W)+1 801 277 2603 电子邮件:忘了它 http://web.torek.net/torek/index.html 阅读电子邮件就像在垃圾邮件中搜索食物一样,感谢垃圾邮件发送者。 Chris Torek写道: 文章< 11 ********************** @ m79g2000cwm.googlegroups .com> < gc *** @ trancer.comwrote: typedef struct {...} volatile my_struct; 一般来说,我建议不要制作无名结构类型和 然后给它们别名。混合使用时,我建议更加谨慎。 " volatile"请参阅< http://web.torek.net/torek/c/types2.html>。 (但这些都不是问题。) 克里斯,谢谢你的帮助。感谢您对无名 结构类型的建议。这有点道理。我明天醒来时会重新阅读你的网站 。关于volatile修饰符,你会建议将它放在结构的每个元素上而不是结构本身吗? > //创建指向两个结构数组的指针 my_struct(* my_struct_ptr)[2]; [其余代码剪断] 你声明一个指向数组的指针在这里,正如你的评论 暗示;但是你不希望* a指向数组的指针,你想要 a指向元素的指针。这是因为一个指向T的指针,对于某些类型为T的b / b,通常 - 甚至可能是通常,并且当然在你的情况下为 - 点对于T型的许多元素中的第一个,那个 在一个数组中。 指向T的阵列N的指针。指向许多数组数组中的第一个 数组。在这种情况下,你指向 指针指向第一个T的数组2,所以p [i]是 我是' ; T的数组2。见< http://web.torek.net/torek/c/pa.html>。 我明白你在说什么。我明天会在工作时给你一个机会。我想你说我需要做的是 以下: my_struct(* mystruct_ptr)[1]; 然后将指针视为任何其他指针。就像我之前说过的那样, 明天我会重新阅读你的网页,希望能够获得更多的好处。 再次感谢您的帮助, Greg 2006年8月10日16:16:04 -0700, gc***@trancer.com 写道: >我无法弄清楚如何声明指向结构数组的指针并用值初始化指针。我看起来是 澄清你的术语。指向数组的短语指针 经常被误用来表示指向数组第一个元素的指针。 对于类型T,这将被编码为T * ptr。从技术上讲, 是正确的,指向T类型数组的指针将被编码为T(* ptr)[N] 其中N将是一个自定义值(编译时间常数)。哪个 你真的想要吗? 以下代码涉及第二种类型。但是,由于 第一种类型允许您更轻松地逐步浏览阵列(更少 打字),第二种类型的使用频率要低得多。 >在这个小组的旧帖子中,尝试了一个看似合理的解决方案,但它没有正常工作。这是我想要完成的一个简单示例: //我有一个我想要访问的硬件外围设备 //两个端口。每个端口有10个连续的 //寄存器。创建一个结构定义, //定义一个端口。 typedef struct { long long1; long long2; long long3; long long4; long long5; long long6; long long7; long long8; long long9; long long10; } volatile my_struct; 在声明的第一行输入单词typedef和struct 之间是不是很不稳定? > //创建一个指向两个结构数组的指针 my_struct(* my_struct_ptr)[2]; 这确实定义了一个指向2结构数组的指针。 > #define PERIPHERAL_BASEADDR 0xA0000000 int main(int argc,char ** argv) { //这是我想出来初始化的语法/> //指针。请记住,在这个例子中甚至不需要这行代码 //来说明编译器在寻址方面的行为。 //。 br /> my_struct_ptr =(my_struct(*)[])PERIPHERAL_BASEADDR; 如果你将下标从[]更改为[2],它应该在语法上 正确。 > printf("& my_struct_ptr [0] =%08X \ n", & my_struct_ptr [0] ); 这里你保证对应于%X的参数将具有int类型。 你实际上传递了一个带有类型指针的参数到数组2 struct。 (&和[0]基本上相互抵消。)这个 调用未定义的行为。如果要打印 指针的值,请使用%p并将值转换为void *。 > printf("& my_struct_ptr [1] =%08X \ n", & my_struct_ptr [1]); printf(" sizeof(my_struct)=%x \ n", sizeof(my_struct)); sizeof计算为size_t,不必是int。如果你想要使用%x,则将其投射。 > printf(" sizeof( my_struct_ptr [0])=%x \ n", sizeof(my_struct_ptr [0])); printf(" sizeof(my_struct_ptr [ 1])=%x \ n", sizeof(my_struct_ptr [1])); printf("&(my_struct_ptr [0 ] - > long1)=%08X \ n", &(my_struct_ptr [0] - > long1)); printf( "&(my_struct_ptr [0] - > long10)=%08X \ n", &(my_struct_ptr [0] - > long10)); printf("&(my_struct_ptr [1] - > long1)=%08X \ n", &(my_struct_ptr [1] - > long1) ); printf("&(my_struct_ptr [1] - > long10)=%08X \ n", &(my_struct_ptr [1] - > long10)); } 这是程序的输出: & my_struct_ptr [0] = A00000 00 & my_struct_ptr [1] = A0000050 sizeof(my_struct)= 28 sizeof(my_struct_ptr [0])= 50 sizeof(my_struct_ptr [1])= 50 &(my_struct_ptr [0] - > long1)= A0000000 &(my_struct_ptr [0] - > long10)= A0000024 &(my_struct_ptr [1] - > long1 )= A0000050 &(my_struct_ptr [1] - > long10)= A0000074 它正确计算typedef'结构的大小,但它报告了数组中每个元素的大小是它的两倍大 不,它不是。 my_struct的大小为40字节(十六进制28)。 my_struct_ptr是一个指向2 struct数组的指针。 my_struct_ptr [0] 是它指向的数组。由于数组有两个元素,因此数组的大小为44字节(十六进制50)。因此我在 开始问的问题。您希望指针指向数组的元素, 而不是数组本身。 >应该是。如果数组中有三个元素,则每个元素的大小是3倍。在此示例中,第二个的long1 您从未询问过该数组元素的大小。为此你需要编码my_struct_ptr [0] [0]。 >元素应该在地址A0000028。 它是。你从未要求过该变量的地址。该变量的名称 是my_struct_ptr [0] [1] .long1。放一个&在前面 获取其地址。 您的代码 - &(my_struct_ptr [1] - > long1) - 评估如下: my_struct_ptr是一个指向struct数组的指针 my_struct_ptr [1]是它指向的第二个数组(下一个数组 经过第一个) my_struct_ptr [1]是一个数组表达式。像往常一样(当不是sizeof或&的 操作数时),数组表达式求值为数组的第一个元素的地址 ,其中类型指针指向element。由于 数组是一个struct数组,因此计算结果为第二个数组的第一个结构的地址或者& my_struct_ptr [1] [0]的类型 指向struct的指针。 my_struct_ptr [1] - > long1是这个结构的第一个元素 实际上是第三个结构开始在你的特殊地址。 第一个结构是my_struct_ptr [0] [0],第二个是[0] [1],[1] [0] 是第三个。 > 我正在使用GCC编译器,但我不认为它是编译器中的错误。我在Visual C ++中尝试了它并获得了相同的结果。 问题你是选择使用指向数组的指针而不是指向数组第一个元素的指针。 > 我试过使用指向单个结构的指针并像使用数组一样使用它。只有一半有效,因为编译器并没有将它看作是一个结构数组,因此你也无法访问结构和索引的元素。 是的,可以。显示你的代码,这样我们就能看出你做错了什么。 删除电子邮件的del I am having trouble figuring out how to declare a pointer to an arrayof structures and initializing the pointer with a value. I''ve lookedat older posts in this group, and tried a solution that lookedsensible, but it didn''t work right. Here is a simple example of whatI''m trying to accomplish:// I have a hardware peripheral that I''m trying to access// that has two ports. Each port has 10 sequential// registers. Create a structure definition that// defines a single port.typedef struct {long long1;long long2;long long3;long long4;long long5;long long6;long long7;long long8;long long9;long long10;} volatile my_struct;// Create a pointer to an array of two structuresmy_struct (*my_struct_ptr)[2];#define PERIPHERAL_BASEADDR 0xA0000000int main(int argc, char **argv){// This is the syntax I came up with to initialize// the pointer. Keep in mind that this line of code// isn''t even needed in this example to illustrate the// behavior of the compiler with respect to addressing.my_struct_ptr = (my_struct (*)[])PERIPHERAL_BASEADDR;printf("&my_struct_ptr[0] = %08X\n",&my_struct_ptr[0]);printf("&my_struct_ptr[1] = %08X\n",&my_struct_ptr[1]);printf("sizeof(my_struct) = %x\n",sizeof(my_struct));printf("sizeof(my_struct_ptr[0]) = %x\n",sizeof(my_struct_ptr[0]));printf("sizeof(my_struct_ptr[1]) = %x\n",sizeof(my_struct_ptr[1]));printf("&(my_struct_ptr[0]->long1) = %08X\n",&(my_struct_ptr[0]->long1));printf("&(my_struct_ptr[0]->long10) = %08X\n",&(my_struct_ptr[0]->long10));printf("&(my_struct_ptr[1]->long1) = %08X\n",&(my_struct_ptr[1]->long1));printf("&(my_struct_ptr[1]->long10) = %08X\n",&(my_struct_ptr[1]->long10));}Here is the output of the program:&my_struct_ptr[0] = A0000000&my_struct_ptr[1] = A0000050sizeof(my_struct) = 28sizeof(my_struct_ptr[0]) = 50sizeof(my_struct_ptr[1]) = 50&(my_struct_ptr[0]->long1) = A0000000&(my_struct_ptr[0]->long10) = A0000024&(my_struct_ptr[1]->long1) = A0000050&(my_struct_ptr[1]->long10) = A0000074It correctly calculates the size of the typedef''d structure, but itreports the size of each element of the array as twice as big as itshould be. If there are three elements in the array, then the size ofeach element is 3 times as big. In this example, long1 of the secondelement should be at address A0000028.I am using the GCC compiler, but I don''t think it is a bug in thecompiler. I tried it in Visual C++ and got the same results.I tried to use a pointer to a single structure and use it like anarray. That only half works because the compiler doesn''t see it as anarray of structures and therefore you can''t access the elements of thestructure and index as well.Any help would be appreciated.Thanks,Greg 解决方案 In article <11**********************@m79g2000cwm.googlegroups .com><gc***@trancer.comwrote:>typedef struct { ... } volatile my_struct;In general, I advise against making nameless structure types andthen giving them aliases. I suggest even more caution when mixing"volatile" in. See <http://web.torek.net/torek/c/types2.html>.(Neither of these is the problem though.)>// Create a pointer to an array of two structuresmy_struct (*my_struct_ptr)[2];[rest of code snipped]You declare a "pointer to an array" here, just as your commentimplies; but you do not *want* a "pointer to an array", you wanta "pointer to an element". This is because a "pointer to T", forsome type T, often -- perhaps even "usually", and certainly inyour case -- points to the first of many elements of type "T" thatare in an array. A "pointer to array N of T" points to the firstarray in an array of many arrays. In this case, you are makingthe pointer point to the first "array 2 of T", so that p[i] isthe i''th "array 2 of T". See <http://web.torek.net/torek/c/pa.html>.--In-Real-Life: Chris Torek, Wind River SystemsSalt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603email: forget about it http://web.torek.net/torek/index.htmlReading email is like searching for food in the garbage, thanks to spammers.Chris Torek wrote:In article <11**********************@m79g2000cwm.googlegroups .com> <gc***@trancer.comwrote:typedef struct { ... } volatile my_struct;In general, I advise against making nameless structure types andthen giving them aliases. I suggest even more caution when mixing"volatile" in. See <http://web.torek.net/torek/c/types2.html>.(Neither of these is the problem though.)Chris, thanks for your help. Thanks for your advice about namelessstructure types. That makes some sense. I''ll re-read your websitetomorrow when I''m more awake. About the volatile modifier, would yourecommend placing it on each of the elements of the structure ratherthan the structure itself?>// Create a pointer to an array of two structuresmy_struct (*my_struct_ptr)[2];[rest of code snipped]You declare a "pointer to an array" here, just as your commentimplies; but you do not *want* a "pointer to an array", you wanta "pointer to an element". This is because a "pointer to T", forsome type T, often -- perhaps even "usually", and certainly inyour case -- points to the first of many elements of type "T" thatare in an array. A "pointer to array N of T" points to the firstarray in an array of many arrays. In this case, you are makingthe pointer point to the first "array 2 of T", so that p[i] isthe i''th "array 2 of T". See <http://web.torek.net/torek/c/pa.html>.I understand what you''re saying. I''ll give it a shot tomorrow when I''mback at work. I think you''re saying that what I need to do is thefollowing:my_struct (*mystruct_ptr)[1];Then just treat the pointer as any other pointer. Like I said before,I''ll re-read your web pages tomorrow and hopefully get even more out ofit.Thanks again for your help,Greg On 10 Aug 2006 16:16:04 -0700, gc***@trancer.com wrote:>I am having trouble figuring out how to declare a pointer to an arrayof structures and initializing the pointer with a value. I''ve lookedClarify your terminology. The phrase pointer to an array isfrequently misused to mean pointer to the first element of the array.For a type T, this would be coded as T *ptr. To be technicallycorrect, a pointer to an array of type T would be coded as T (*ptr)[N]where N would be a self-defining value (compile time constant). Whichdo you really want?Your following code deals with the second type. However, since thefirst type allows you to step through the array more easily (lesstyping), the second type is much less frequently used.>at older posts in this group, and tried a solution that lookedsensible, but it didn''t work right. Here is a simple example of whatI''m trying to accomplish:// I have a hardware peripheral that I''m trying to access// that has two ports. Each port has 10 sequential// registers. Create a structure definition that// defines a single port.typedef struct { long long1; long long2; long long3; long long4; long long5; long long6; long long7; long long8; long long9; long long10;} volatile my_struct;Doesn''t the volatile need to be between the words typedef and structon the first line of the declaration?>// Create a pointer to an array of two structuresmy_struct (*my_struct_ptr)[2];This does define a pointer to an array of 2 struct.>#define PERIPHERAL_BASEADDR 0xA0000000int main(int argc, char **argv){ // This is the syntax I came up with to initialize // the pointer. Keep in mind that this line of code // isn''t even needed in this example to illustrate the // behavior of the compiler with respect to addressing. my_struct_ptr = (my_struct (*)[])PERIPHERAL_BASEADDR;If you change the subscript from [] to [2] it should be syntacticallycorrect.> printf("&my_struct_ptr[0] = %08X\n", &my_struct_ptr[0]);Here you promise the argument corresponding to %X will have type int.You actually pass the an argument that has type pointer to array of 2struct. (The & and the [0] basically cancel each other.) Thisinvokes undefined behavior. If you want to print the value of apointer, use %p and cast the value to a void*.> printf("&my_struct_ptr[1] = %08X\n", &my_struct_ptr[1]); printf("sizeof(my_struct) = %x\n", sizeof(my_struct));sizeof evaluates to a size_t which need not be an int. Cast it if youwant to use %x.> printf("sizeof(my_struct_ptr[0]) = %x\n", sizeof(my_struct_ptr[0])); printf("sizeof(my_struct_ptr[1]) = %x\n", sizeof(my_struct_ptr[1])); printf("&(my_struct_ptr[0]->long1) = %08X\n", &(my_struct_ptr[0]->long1)); printf("&(my_struct_ptr[0]->long10) = %08X\n", &(my_struct_ptr[0]->long10)); printf("&(my_struct_ptr[1]->long1) = %08X\n", &(my_struct_ptr[1]->long1)); printf("&(my_struct_ptr[1]->long10) = %08X\n", &(my_struct_ptr[1]->long10));}Here is the output of the program:&my_struct_ptr[0] = A0000000&my_struct_ptr[1] = A0000050sizeof(my_struct) = 28sizeof(my_struct_ptr[0]) = 50sizeof(my_struct_ptr[1]) = 50&(my_struct_ptr[0]->long1) = A0000000&(my_struct_ptr[0]->long10) = A0000024&(my_struct_ptr[1]->long1) = A0000050&(my_struct_ptr[1]->long10) = A0000074It correctly calculates the size of the typedef''d structure, but itreports the size of each element of the array as twice as big as itNo it doesn''t. my_struct has a size of 40 bytes (hex 28).my_struct_ptr is a pointer to an array of 2 struct. my_struct_ptr[0]is the array it points to. Since the array has two elements, the sizeof the array is 80 bytes (hex 50). Hence the question I asked at thebeginning. You want the pointer to point to an element of the array,not the array itself.>should be. If there are three elements in the array, then the size ofeach element is 3 times as big. In this example, long1 of the secondYou never asked for the size of an element of the array. To do so youwould have to code my_struct_ptr[0][0].>element should be at address A0000028.It is. You never asked for the address of that variable. The namefor that variable is my_struct_ptr[0][1].long1. Put an & in front toget its address.Your code - &(my_struct_ptr[1]->long1) - evaluates as follows:my_struct_ptr is a pointer to an array of structmy_struct_ptr[1] is the second array it points (the next arraypast the first)my_struct_ptr[1] is an array expression. As usual (when not theoperand of sizeof or &), the array expression evaluates to the addressof the first element of the array with type pointer to element. Sincethe array is an array of struct, this evaluates to the address of thefirst struct of the second array or &my_struct_ptr[1][0] with typepointer to struct.my_struct_ptr[1]->long1 is the first element of this struct whichis actually the third struct starting at your special address. Thefirst struct is my_struct_ptr[0][0], the second is [0][1], and [1][0]is the third.>I am using the GCC compiler, but I don''t think it is a bug in thecompiler. I tried it in Visual C++ and got the same results.The "problem" is you chose to use a pointer to array rather thanpointer to first element of array.>I tried to use a pointer to a single structure and use it like anarray. That only half works because the compiler doesn''t see it as anarray of structures and therefore you can''t access the elements of thestructure and index as well.Yes it can. Show your code so we can see what you did wrong.Remove del for email 这篇关于需要帮助声明指向结构数组的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 19:27