Constructor for structs in C

前端 未结 6 1214
南旧
南旧 2021-02-01 01:48

Given:

struct objStruct {
    int id;
    int value;
};

typedef struct objStruct Object;

Is there a shortcut to allocate and initialize the ob

6条回答
  •  醉话见心
    2021-02-01 02:24

    You really have to distinguish initialization of static or auto variables and dynamic allocation on the head. For the first, do named initializers, for the second a well specified init function.

    All that can be nicely packed into macros do give you an easy static/auto intialization and something similar to new in C++.

提交回复
热议问题