0

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
  • 1
    please refer to https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const – Deng Haijun Apr 25 '16 at 23:03
  • 3
    please don't needlessly tag C questions as c++. – Brian Bi Apr 25 '16 at 23:05
  • 1
    actually compiles fine as C but fails as c++ – pm100 Apr 25 '16 at 23:06
  • This has been edited so many times since it was first posted that it's getting a bit absurd. As of this comment, the current version compiles without errors or warnings. Should we assume that your problem has been solved? – Tom Karzes Apr 25 '16 at 23:09
  • 2
    You are making something up. The code you provided compiles perfectly in C and C++ (regardless of whether the cast is there or not). It does not produce "initializer element is not constant" error. Moreover, this error sounds like something from C, while the question is tagged [C++]. Why is it tagged [C++], while you are talking about C in the text? – AnT stands with Russia Apr 25 '16 at 23:09
  • The code as-is (was) [**compiles correctly**](https://ideone.com/Hmzfac). If your intent was to declare an immutable pointer to mutable data, you succeeded. If your intent was to declare a mutable pointer to immutable data, it should be `struct s const *c = &a;` If your intent was to declare an immutable pointer to immutable data, it should be `struct s const * const c = &a;`. If it is something else, please tell *exactly* what you're trying to do. – WhozCraig Apr 25 '16 at 23:09
  • 2
    Voting to close. The problem originally mentioned in the post no longer exists as of the current edit. – Tom Karzes Apr 25 '16 at 23:10
  • I modify the post to make it fit better the situation... – Nicolas Scotto Di Perto Apr 25 '16 at 23:11
  • @M.M My deepest apologies, I removed the wrong tag by accident. – Brian Bi Apr 25 '16 at 23:22
  • @AnT - it was originally tagged as C. Someone removed the tag. – RJM Apr 25 '16 at 23:51

1 Answers1

-1

This compiles

#include <iostream>
using namespace std;
struct s {
  int f;
} a = { 1 };

struct s  const * c = &a;
int main() {
    // your code goes here
    return 0;
}
pm100
  • 48,078
  • 23
  • 82
  • 145
  • Compiles, but is no longer what was asked for: Now it's a non-const pointer to a const struct. -1 – Daniel Jour Apr 25 '16 at 23:10
  • I know - but it shows that you can have consts that include link time resolved addresses - this is interesting, and it points to where the problem lies. At least I got the poster halfway to the answer – pm100 Apr 25 '16 at 23:51