i define a structure that contains a constant member.
the problem is, when i use malloc to make such a struct, i fail to initialize its const member, for example:

struct C {
    int* const p;
};

int main()
{
    int i = 0;
    struct C s = { &i }; // ok

    struct C* d = (struct C*)malloc(sizeof(struct C));
    d->p = &i; // error
    free(d);
    return 0;
}

is there any way i can do it?
ps: i use MinGW GCC.

const is used to make something constant, meaning that its value should never change, as such you cant dynamically allocate an array for a constant value. It has to either be a regular const array with the values filled in or a dynamic array without const.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.