G
gcary
I am having trouble figuring out how to declare a pointer to an array
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that looked
sensible, but it didn't work right. Here is a simple example of what
I'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 structures
my_struct (*my_struct_ptr)[2];
#define PERIPHERAL_BASEADDR 0xA0000000
int 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] = 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
It correctly calculates the size of the typedef'd structure, but it
reports the size of each element of the array as twice as big as it
should be. If there are three elements in the array, then the size of
each element is 3 times as big. In this example, long1 of the second
element should be at address A0000028.
I am using the GCC compiler, but I don't think it is a bug in the
compiler. 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 an
array. That only half works because the compiler doesn't see it as an
array of structures and therefore you can't access the elements of the
structure and index as well.
Any help would be appreciated.
Thanks,
Greg
of structures and initializing the pointer with a value. I've looked
at older posts in this group, and tried a solution that looked
sensible, but it didn't work right. Here is a simple example of what
I'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 structures
my_struct (*my_struct_ptr)[2];
#define PERIPHERAL_BASEADDR 0xA0000000
int 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] = 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
It correctly calculates the size of the typedef'd structure, but it
reports the size of each element of the array as twice as big as it
should be. If there are three elements in the array, then the size of
each element is 3 times as big. In this example, long1 of the second
element should be at address A0000028.
I am using the GCC compiler, but I don't think it is a bug in the
compiler. 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 an
array. That only half works because the compiler doesn't see it as an
array of structures and therefore you can't access the elements of the
structure and index as well.
Any help would be appreciated.
Thanks,
Greg