I'd like to create a const
pointer to some data in C:
struct s {
int f;
} a = { 1 };
struct s * const c = (struct s * const)&a;
But this leds to the error error: initializer element is not constant
.
I guess it is because an adress is not a compile-constant, is this still possible in some ways?
Are const
pointers only explicitly assignable? (like void * const * x = 0xbfc0d878
)
Edit: I apoligize for the poor description of the problem I gave, this is actually works in C. I have tried to rewrite a simpler version of the part which doesn't work but it works so I think it's better to put the actual code, it's not that long.
In file protocol.h:
#define NRING 2
typedef struct entity {
char id[9]; // 8th char max, 9th is 0
char ip_self[16];
uint16_t udp;
uint16_t tcp;
char ip_next[NRING][16];
uint16_t port_next[NRING];
char mdiff_ip[NRING][16];
uint16_t mdiff_port[NRING];
} entity;
extern entity * const ent;
In file protocol.c:
entity _ent_;
entity * const ent = (entity * const)&_ent_;
In file _protocol_interface.h_:
#include "protocol.h"
typedef struct info_t {
const char id[9];
const char ip_self[16];
const uint16_t udp;
const uint16_t tcp;
const char ip_next[NRING][16];
const uint16_t port_next[NRING];
const char mdiff_ip[NRING][16];
const uint16_t mdiff_port[NRING];
} info_t;
extern info_t * const info;
In file _protocol_interface.c_:
info_t * const info = (info_t * const)ent;
The error come from _protocol_interface.c_:
error: initializer element is not constant
info_t * const info = (info_t * const)ent;
Edit2: This works:
info_t * const info = (info_t * const)&_ent_; // _ent_ is of type entity
While this doesn't
info_t * const info = (info_t * const)ent; // ent is of type entity * const