What's the context of the question?
For example, is this part of some larger program, then how would this be integrated with that program?
There are many "easier" ways, how about some clues as to which might be easiest for you.
--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.