0

I am not able to access const array sfp_addr and assign it to another const array variable. Here is my code (it has been edited for brevity):

#define SFP_NUM 2
 
const int sfp_addr[SFP_NUM] =
{
    0x50, 0x51
};


struct sfp
{
  const int (*addr)[SFP_NUM];
};

struct sfp_stat
{
  struct sfp sfp;
};

int sfp_read(struct sfp_stat *stat) 
{
    stat->sfp.addr = &sfp_addr[1];
}

The compiler is catching this error: In function 'sfp_read', 21: error: assignment from incompatible pointer type [-Werror]

Can anyone explain why I am seeing this error and how to fix this?

Sedmaister
  • 475
  • 1
  • 4
  • 11

1 Answers1

-2
 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]
arfneto
  • 1,227
  • 1
  • 6
  • 13
  • 2
    One cannot cast a pointer to int in the middle of array to a pointer to whole array and expect it work without invoking UB – tstanisl Jul 19 '23 at 23:01
  • A `cast` is just it. `C` hopes you know what you are writing code for. What is undefined in a cast? – arfneto Jul 19 '23 at 23:17
  • @arfneto https://stackoverflow.com/a/4810460/5878272 – Fredrik Jul 20 '23 at 10:23
  • @tstanisl a `cast` is just a `cast`. The author of the code must consider side effects. If not IMHO it is just a logic error as any other and sure the program behaviour then is undefined. Is is not the cast. On error, one can also divide by zero. It is no the division's fault. As a side note: today `static_cast` has 11 notes on `cppreference`. And 13 defect reports. It is dangerous, but it is there for a reason, like `memcpy`. – arfneto Jul 20 '23 at 14:16