How to initialize static pointer with malloc in C?

后端 未结 5 608
臣服心动
臣服心动 2020-12-17 22:27

I\'m trying to initiate a static variable (inside a function) with malloc in C, but I\'m getting the \"initializer not constant error\". I know that I can\'t initiate a stat

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 23:20

    static int *p = NULL;
    if(!p) p = (int *)malloc(sizeof(int));
    

提交回复
热议问题