stat->sfp.addr = &sfp_addr[1];
why you are seeing this
addr
inside sfp
is const int(*) [2]
, a pointer to a const
array of 2 int
.
&
comes after []
in terms of precedence, so &sfp_addr[1]
is const int(*)
--- C
has pointer arithmetic.
how to fix this
You can use a cast:
stat->sfp.addr = (int(*)[SFP_NUM]) &sfp_addr[1];
Since sfp_add
is const int[2]
your code is on for trouble if you try to change values anyway.
EXAMPLE
Here you see sfp_read
changed in order to show values of 2 pairs, one constant, one not constant. In main
values are changed...
#define SFP_NUM 2
#include <stdio.h>
const int sfp_addr[SFP_NUM] = {80, 81};
int sfp_addr_nc[SFP_NUM] = {82, 83};
struct sfp
{
const int (*addr)[SFP_NUM];
};
struct sfp_nc
{ // address of non constant array
int (*addr)[SFP_NUM];
};
struct sfp_stat
{
struct sfp sfp;
struct sfp_nc sfp2;
};
int sfp_read(struct sfp_stat* stat)
{
printf(
"\
1st array has two int:\t[%d,%d]\n\
2nd array has 2 more int:\t[%d,%d]\n",
*(stat->sfp.addr)[0], (*stat->sfp.addr)[1],
*(stat->sfp2.addr)[0], (*stat->sfp2.addr)[1]);
return 0;
}
int main(void)
{
struct sfp_stat one = {
.sfp = (int(*)[SFP_NUM]) &sfp_addr,
.sfp2 = (int(*)[SFP_NUM]) &sfp_addr_nc};
sfp_read(&one);
(*(one.sfp2.addr))[0] = 42;
(*(one.sfp2.addr))[1] = 4242;
sfp_read(&one);
return 0;
};
output
1st array has two int: [80,81]
2nd array has 2 more int: [82,83]
1st array has two int: [80,81]
2nd array has 2 more int: [42,4242]