#Shorter way to initialise and allocate structs

8 messages · Page 1 of 1 (latest)

tender zenith
#

I find myself needing to do quite often the following pattern:

T* some = malloc(sizeof(T));
some->foo = value;

This gets really clunky when my structs have lots of fields, so I'm curious is there a way to do this more efficiently?

opal fiberBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

drowsy radish
#

The only way to improve really would be to make a function

#
T* new_T(int value) {
    T* some = malloc(sizeof(T));
    some->foo = value;
    return some;
}
tender zenith
#

I was planning to do that, but asked in case there was a better way

#

Thx!

drowsy radish
#

Happy to help

opal fiberBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.