How to initialize const in a struct in C (with malloc)

后端 未结 6 427
谎友^
谎友^ 2020-11-28 08:46

I have tried;

void *malloc(unsigned int);
struct deneme {
    const int a = 15;
    const int b = 16;
};

int main(int argc, const char *argv[])
{
    struct         


        
6条回答
  •  醉梦人生
    2020-11-28 09:25

    Have you tried to do like this:

    int main(int argc, const char *argv[])
    {
        struct deneme mydeneme = { 15, 20 };
        struct deneme *pmydeneme = malloc(sizeof(struct deneme));
        memcpy(pmydeneme, &mydeneme , sizeof(mydeneme));
        return 0;
    }
    

    I have not tested but the code seems correct

提交回复
热议问题